语言运行速度的一次测试
2013-01-08 18:58
176 查看
做了一个实验,比较Python、C、Go的运行速度,结果居然是... ...
代码目标:计算斐波纳契数列
C代码:
Go代码:
Python代码:
测试机CPU信息:
测试结果:
Python:
C:
Go:
我每种语言都跑过5次以上,运行时间误差在0.01秒以内。
Go比C还快?这是啥道理?
代码目标:计算斐波纳契数列
C代码:
int fib(int n){ if (n < 2) return n; else return fib(n - 1) + fib(n - 2); } int main() { fib(40); return 0; }
Go代码:
package main func fib(n int) int { var r int if n < 2 { r = n } else { r = fib(n - 1) + fib(n - 2) } return r } func main(){ fib(40) }
Python代码:
def fib(n): if n < 2: return n else: return fib(n - 1) + fib(n - 2) fib(40)
测试机CPU信息:
[liuxd@liuxd]$ [master] cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz stepping : 10 microcode : 0xa0b cpu MHz : 1995.000 cache size : 6144 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm tpr_shadow vnmi flexpriority bogomips : 5984.93 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz stepping : 10 microcode : 0xa0b cpu MHz : 2995.000 cache size : 6144 KB physical id : 0 siblings : 2 core id : 1 cpu cores : 2 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm dtherm tpr_shadow vnmi flexpriority bogomips : 5984.93 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management:
测试结果:
Python:
real 0m52.221s user 0m51.999s sys 0m0.025s
C:
real 0m2.260s user 0m2.249s sys 0m0.001s
Go:
real 0m1.534s user 0m1.530s sys 0m0.001s
我每种语言都跑过5次以上,运行时间误差在0.01秒以内。
Go比C还快?这是啥道理?
相关文章推荐
- Go语言日趋成熟 运行速度接近C语言实现程序
- 在jmeter测试mysql中如何一次运行多条sql语句
- windows平台上的PHP运行在IIS,Apache,Nginx的性能和速度测试日志
- zw.delphi不同版本程序运行速度测试
- 卷积神经网络的训练和测试(针对电脑内存比较小的,运行速度比较慢的)
- C#基础语言运行效率测试报告
- javascript测试函数运行速度
- Pycaffe-简单测试caffe模型的分类效果和运行速度
- 在jmeter测试mysql中如何一次运行多条sql语句
- 利用timer()测试程序运行速度starttime=timer()
- ASP程序运行速度测试
- 一起talk C栗子吧(第三十五回:C语言实例--测试程序运行时间)
- 解决使用robot framework + selenium测试时,RIDE运行一次后不显示log的问题
- php测试程序运行速度
- ASP程序运行速度测试
- 关于JAVA,特点,历史,编译式的语言&解释式的语言,什么是java?JDK?DOS?一次编译到处运行原理。
- delphi 2007 vs E语言 vs C#运行速度
- 一次运行多个测试类2-1个测试类重复测试
- 芯片测试太耗时?新一代TetraMAX II工具将运行速度提高一个数量级
- 利用UDPSocket模拟一次对话,只能运行依次,多次执行还在测试中.跪求大神建议....