- 注册时间
- 2010-7-23
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 851
- 在线时间
- 小时
|
发表于 2010-8-9 18:25:19
|
显示全部楼层
lua是用纯标准C写的。
一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。
俺不知道C是否支持运算符重载
wayne 发表于 2010-8-9 16:50
Forcal是用C/C++写的,核心库FORCAL32W.DLL文件大小:约130K;不使用MSVC运行库的静态库版本,约260K~300K。
以下程序的速度,lua要差一些:- for(x=0.0;x<=1.0;x=x+0.0011)
- {
- for(y=1.0;y<=2.0;y=y+0.0011)
- {
- sum=sum+cos(1.0-sin(1.2*pow(x+0.1,y/2.0-x)+cos(1.0-sin(1.2*pow(x+0.2,y/3.0-x))))-cos(1.0-sin(1.2*pow(x+0.3,y/4.0-x)))-cos(1.0-sin(1.2*pow(x+0.4,y/5.0-x)+cos(1.0-sin(1.2*pow(x+0.5,y/6.0-x))))-cos(1.0-sin(1.2*pow(x+0.6,y/7.0-x)))));
- }
- }
复制代码 lua代码1:- #!/usr/local/bin/lua
-
- -- By forcal
-
- function sum()
- local t = os.clock()
-
- local x=0
- local y=0
- local sum=0
- for x=0,1,0.0011 do
- for y=1,2,0.0011 do
- sum=sum+math.cos(1-math.sin(1.2*(x+0.1)^(y/2-x)+math.cos(1-math.sin(1.2*(x+0.2)^(y/3-x))))-math.cos(1-math.sin(1.2*(x+0.3)^(y/4-x)))-math.cos(1-math.sin(1.2*(x+0.4)^(y/5-x)+math.cos(1-math.sin(1.2*(x+0.5)^(y/6-x))))-math.cos(1-math.sin(1.2*(x+0.6)^(y/7-x)))))
- end
-
- end
- io.write(sum)
-
- io.write(string.format("\nTime Elapsed %f\n", os.clock() - t))
- end
-
- sum()
复制代码 结果:
19160.536601703
Time Elapsed 3.109000
Forcal代码1:- mvar:
- t0=sys::clock(),
- sum=0,
- x=0,
- (x<=1).while{
- y=1,
- (y<=2).while{
- sum=sum+cos(1-sin(1.2*(x+0.1)^(y/2-x)+cos(1-sin(1.2*(x+0.2)^(y/3-x))))-cos(1-sin(1.2*(x+0.3)^(y/4-x)))-cos(1-sin(1.2*(x+0.4)^(y/5-x)+cos(1-sin(1.2*(x+0.5)^(y/6-x))))-cos(1-sin(1.2*(x+0.6)^(y/7-x))))),
- y=y+0.0011
- },
- x=x+0.0011
- },
- printff{"\r\n{1,r}\r\nTime Elapsed {2,r}\r\n",sum,[sys::clock()-t0]/1000};
复制代码 结果:
19160.536601703152
Time Elapsed 1.984
lua代码2:- #!/usr/local/bin/lua
-
- -- By forcal
-
- function f(x,y)
- return math.cos(1-math.sin(1.2*(x+0.1)^(y/2-x)+math.cos(1-math.sin(1.2*(x+0.2)^(y/3-x))))-math.cos(1-math.sin(1.2*(x+0.3)^(y/4-x)))-math.cos(1-math.sin(1.2*(x+0.4)^(y/5-x)+math.cos(1-math.sin(1.2*(x+0.5)^(y/6-x))))-math.cos(1-math.sin(1.2*(x+0.6)^(y/7-x)))))
- end
-
- function sum()
- local t = os.clock()
-
- local x=0
- local y=0
- local sum=0
- for x=0,1,0.0011 do
- for y=1,2,0.0011 do
- sum=sum+f(x,y)
- end
-
- end
- io.write(sum)
-
- io.write(string.format("\nTime Elapsed %f\n", os.clock() - t))
- end
-
- sum()
复制代码 结果:
19160.536601703
Time Elapsed 3.250000
Forcal代码2:- mvar:
- f(x,y)=cos(1-sin(1.2*(x+0.1)^(y/2-x)+cos(1-sin(1.2*(x+0.2)^(y/3-x))))-cos(1-sin(1.2*(x+0.3)^(y/4-x)))-cos(1-sin(1.2*(x+0.4)^(y/5-x)+cos(1-sin(1.2*(x+0.5)^(y/6-x))))-cos(1-sin(1.2*(x+0.6)^(y/7-x)))));
- t0=sys::clock(),
- sum=0,
- x=0,
- (x<=1).while{
- y=1,
- (y<=2).while{
- sum=sum+f(x,y),
- y=y+0.0011
- },
- x=x+0.0011
- },
- printff{"\r\n{1,r}\r\nTime Elapsed {2,r}\r\n",sum,[sys::clock()-t0]/1000};
复制代码 结果:
19160.536601703152
Time Elapsed 2.0779999999999998
以上代码1的C++耗时约1.33秒。
以上代码Forcal只输给了matlab的矢量化代码,因为matlab的矢量化代码比C++还快,小于1秒。 |
|