分別用c, cs 和 vala 完成同樣的運算:Code
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("c for begin\n");
int count = 1000000000;
int i;
for(i=0;i<count;i++)
{
;
}
printf("c for end\n");
printf("int add begin\n");
int ncount = 1000000000;
int n = 0;
int j;
for(j=0;j<ncount;j++)
{
int a = 3;
int b = 4;
n = a + b;
}
printf("%d", n);
printf("int add end\n");
printf("float add begin\n");
int fcount = 10000000;
float f = 0.0f;
int k;
for(k=0;k<fcount;k++)
{
float a = 3.0f;
float b = 4.0f;
f = a + b;
}
printf("%f", f);
printf("float add end\n");
return 0;
}
測試結果:Code
user@mojo:~/Projects/performance/c$ time ./c
c for begin
c for end
int add begin
7int add end
float add begin
7.000000float add end
real 0m49.264s
user 0m46.450s
sys 0m0.010s
user@mojo:~/Projects/performance/cs$ time mono main.exe
c for begin
c for end
int add begin
7
int add end
float add begin
7
float add end
real 1m37.252s
user 1m31.880s
sys 0m0.135s
user@mojo:~/Projects/performance/vala$ time ./main
c for begin
c for end
int add begin
7int add end
float add begin
7.000000float add end
real 1m14.979s
user 1m10.995s
sys 0m0.020s
ps: 以上測試全部在SmartQ5 (cpu: s3c6410 arm11 667MHz, ram: 128M)上完成, 其間也測試了python和lua,結論是基本上沒有可比性,類死機狀態。
/Files/zhongzf/src.zip