编程语言分形算法大乱斗(可能违反本版版规 如果这样麻烦几位版主帮忙转到水区去 谢谢了)
编程语言分形算法大乱斗(可能违反本版版规 如果这样麻烦几位版主帮忙转到水区去 谢谢了);*****************************************************************************************************************
;作者:zklhp
;Email:zklhp@
;QQ:493165744
;版权所有 转载请保持完整
;*****************************************************************************************************************
本贴无意比较语言优劣 所得结果不足以判断语言效率 只不过是我的一个消遣和娱乐 如有不妥欢迎批评指正
这帖放哪都不好 C区人多放这好了 也有C语言 只不过只占一部分
这个测试是根据
http://www.
这个写的 貌似作者更新了 我用我能找到的编译器重现了一下 基于原文的代码
gcc 4.4.5 (QP MinGW32) 参数 gcc.exe -Wall -O2
程序代码:
#include <stdio.h> #include <time.h> #define BAILOUT 16 #define MAX_ITERATIONS 1000 int mandelbrot(double x, double y) { double cr = y - 0.5; double ci = x; double zi = 0.0; double zr = 0.0; int i = 0; while(1) { i ++; double temp = zr * zi; double zr2 = zr * zr; double zi2 = zi * zi; zr = zr2 - zi2 + cr; zi = temp + temp + ci; if (zi2 + zr2 > BAILOUT) return i; if (i > MAX_ITERATIONS) return 0; } } int main() { long t1 = 0, t2 = 0; t1 = clock(); int x,y; for (y = -39; y < 39; y++) { printf("\n"); for (x = -39; x < 39; x++) { int i = mandelbrot(x/40.0, y/40.0); if (i==0) printf("*"); else printf(" "); } } printf ("\n"); t2 = clock(); double query_time = (t2 - t1)/1000.0; printf ("C Elapsed %0.2f\n", query_time); return 0; }
C Elapsed 0.20
VC 2008 我调的编译参数是 /Ox /Ob2 /Oi /Ot
程序代码:
#include "stdafx.h" //预编译头包含用到的头文件 #define BAILOUT 16 #define MAX_ITERATIONS 1000 int mandelbrot(double x, double y) { double cr = y - 0.5; double ci = x; double zi = 0.0; double zr = 0.0; int i = 0; while(1) { i ++; double temp = zr * zi; double zr2 = zr * zr; double zi2 = zi * zi; zr = zr2 - zi2 + cr; zi = temp + temp + ci; if (zi2 + zr2 > BAILOUT) return i; if (i > MAX_ITERATIONS) return 0; } } int _tmain(int argc, _TCHAR* argv[]) { long t1 = 0, t2 = 0; t1 = clock(); int x,y; for (y = -39; y < 39; y++) { printf("\n"); for (x = -39; x < 39; x++) { int i = mandelbrot(x/40.0, y/40.0); if (i==0) printf("*"); else printf(" "); } } printf ("\n"); t2 = clock(); double query_time = (t2 - t1)/1000.0; printf ("C Elapsed %0.2f\n", query_time); return 0; }
Elapsed 0.27
python 2.6.6 都是默认参数
程序代码:
import sys, time stdout = sys.stdout BAILOUT = 16 MAX_ITERATIONS = 1000 class Iterator: def __init__(self): print 'Rendering...' for y in range(-39, 39): stdout.write('\n') for x in range(-39, 39): i = self.mandelbrot(x/40.0, y/40.0) if i == 0: stdout.write('*') else: stdout.write(' ') def mandelbrot(self, x, y): cr = y - 0.5 ci = x zi = 0.0 zr = 0.0 i = 0 while True: i += 1 temp = zr * zi zr2 = zr * zr zi2 = zi * zi zr = zr2 - zi2 + cr zi = temp + temp + ci if zi2 + zr2 > BAILOUT: return i if i > MAX_ITERATIONS: return 0 t = time.time() Iterator() print '\nPython Elapsed %.02f' % (time.time() - t)
编译版 Elapsed 1.84
解释版 Elapsed 1.88
javascript 这个和解释器有关系了 我用的是firefox的引擎 也就是SpiderMonkey ftp上面下载的最新测试版 Version: JavaScript-C 1.8.5+ 2011-04-16
程序代码:
function mandelbrot(x, y) { var cr = y - 0.5; var ci = x; var zi = 0.0; var zr = 0.0; var i = 0; var BAILOUT = 16; var MAX_ITERATIONS = 1000; while(1) { i++; var temp = zr * zi; var zr2 = zr * zr; var zi2 = zi * zi; zr = zr2 - zi2 + cr; zi = temp + temp + ci; if (zi2 + zr2 > BAILOUT) { return i; } if (i > MAX_ITERATIONS) { return 0; } } } function mandelbrot_run() { var x; var y; output = ""; var date = new Date(); for (y = -39; y < 39; y++) { print(output); output = ""; for (x = -39; x < 39; x++) { var i = mandelbrot(x/40.0, y/40.0); if (i==0) { output += "*"; } else { output += " "; } } } var date2 = new Date(); output += "\nJavaScript Elapsed " + (date2.getTime() - date.getTime()) / 1000; print(output); return false; } mandelbrot_run();
Elapsed 1.647
Emacs lisp 也就是elisp 其实我是查这个的时候偶然找到上面的东东的 这个是正题 因为我想知道到底这玩意效率咋样
GNU Emacs 24.0.95.1 也是测试版 默认参数
程序代码:
(defun iterate (xx yy) (let* ( (BAILOUT 16.0) (MAX_ITERATIONS 1000) (bail_val 0) (cr (- yy 0.5)) (ci xx) (zi 0.0) (zr 0.0) (i 0) ) (while (and (< i MAX_ITERATIONS) (< bail_val BAILOUT)) (setq i (+ 1 i)) (setq temp (* zr zi)) (setq zr2 (* zr zr)) (setq zi2 (* zi zi)) (setq zr (+ (- zr2 zi2) cr)) (setq zi (+ temp temp ci)) (setq bail_val (+ zi2 zr2))) i) ) (defun mandelbrot() (setq yy -39) (while (< yy 39) (setq yy (+ 1 yy)) (setq xx -39) (while (< xx 39) (setq xx (+ 1 xx)) (if (= (iterate (/ xx 40.0) (/ yy 40.0)) 1000) (princ "*") (princ " ") ) ) (princ "\n") )) (setq the-time (cadr (current-time))) (mandelbrot) (princ (format "Elapsed %d" (- (cadr (current-time)) the-time)))
解释版 Elapsed 12
编译版 Elapsed 7或8
不知道怎么得到毫秒。。
讨论
1 加了优化参数的VC不如gcc效率高。。 和我一直的认识不一致
2 python效率确实挺好 看着有点像玩具其实很强大
3 javascript引擎由于经历了历次浏览器战争而得到了很好的优化 如果用chrome的V8引擎或许速度更快 可惜没有像jsshell这样现成的东西
4 elisp效率挺好的 编译能提高性能 大约50%哦
[ 本帖最后由 zklhp 于 2012-4-9 19:31 编辑 ]