赞
踩
本文采用的是【C#】语言进行测试
- using System;
- using System.Diagnostics;
- namespace Action
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Random ra = new Random();
- int count = 1000000;
- DateTime start = DateTime.Now;
- for (int i = 0; i < count; i++)
- {
- switch (ra.Next(10))
- {
- case 0:break;
- case 1:break;
- case 2:break;
- case 3:break;
- case 4:break;
- case 5:break;
- case 6:break;
- case 7:break;
- case 8:break;
- case 9:break;
- default: break;
- }
- }
- DateTime end = DateTime.Now;
- double usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
- Console.WriteLine("耗时:"+(end-start).TotalMilliseconds+"毫秒");
- Console.WriteLine("消耗内存:"+ usedMemory+"M");
- }
- }
- }
- using System;
- using System.Diagnostics;
- namespace Action
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Random ra = new Random();
- int count = 1000000;
- DateTime start = DateTime.Now;
- for (int i = 0; i < count; i++)
- {
- int v = ra.Next(10);
- if (v == 0)
- {
- }
- else if (v == 1)
- {
- }
- else if (v == 2)
- {
- }
- else if (v == 3)
- {
- }
- else if (v == 4)
- {
- }
- else if (v == 5)
- {
- }
- else if (v == 6)
- {
- }
- else if (v == 7)
- {
- }
- else if (v == 8)
- {
- }
- else if (v == 9)
- {
- }
- }
- DateTime end = DateTime.Now;
- double usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
- Console.WriteLine("耗时:" + (end - start).TotalMilliseconds + "毫秒");
- Console.WriteLine("消耗内存:" + usedMemory + "M");
- }
- }
- }
100W次swtich判断,消耗时间34.68ms,消耗内存16.30M
1、在C#语言中,两者效率相差不大,几乎可以忽略不计,在一百万次判断中只是相差2~3毫秒,效率还是相当惊人的。
2、很明显的是【Java】【Python】【C#】三者测试完成后,觉得Java的效率还是最高的。相信,如果换成用Linux服务器效果会更好。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。