编程语言EF速度测试(5):binary-trees
这是一个专门的编程语言/编译器速度测试/对比网站(http://shootout.alioth.debian.org/)给出的题目,
以下是EF源代码:
//binary-trees public class 启动类 <作者 = "liigo"> { const int minDepth = 4; public static main() { int time = 运行环境.取启动时间(); int n = 16; int maxDepth = n; if(minDepth + 2 > n) maxDepth = minDepth + 2; int stretchDepth = maxDepth + 1; int check = (TreeNode.bottomUpTree(0,stretchDepth)).itemCheck(); 控制台.输出行("stretch tree of depth ", stretchDepth, "\t check: ", check); TreeNode longLivedTree = TreeNode.bottomUpTree(0,maxDepth); for (int depth=minDepth; depth<=maxDepth; depth+=2){ int iterations = 1 << (maxDepth - depth + minDepth); check = 0; for (int i=1; i<=iterations; i++){ check += (TreeNode.bottomUpTree(i,depth)).itemCheck(); check += (TreeNode.bottomUpTree(-i,depth)).itemCheck(); } 控制台.输出行((iterations*2), "\t trees of depth ", depth, "\t check: ", check); } 控制台.输出行("long lived tree of depth ", maxDepth, "\t check: ", longLivedTree.itemCheck()); 控制台.输出行("time(ms): ", 运行环境.取启动时间() - time); 控制台.输入文本(); } private class TreeNode { private TreeNode left, right; private int item; public init(int item){ this.item = item; } public static TreeNode bottomUpTree(int item, int depth){ if (depth>0){ return new TreeNode( bottomUpTree(2*item-1, depth-1) , bottomUpTree(2*item, depth-1) , item ); } else { return new TreeNode(item); } } public init(TreeNode left, TreeNode right, int item){ this.left = left; this.right = right; this.item = item; } public int itemCheck(){ // if necessary deallocate here if (left==null) return item; else return item + left.itemCheck() - right.itemCheck(); } } }
这个EF程序,在我的机器上,运行耗时约 108 秒。相应的VC6最佳优化后运行耗时约 6.9 秒。
其它编程语言/编译器的表现,请看这里:http://shootout.alioth.debian.org/gp4/benchmark.php?test=binarytrees&lang=all
针对这次测试,EF表现较差,是开赛以来最差的一次,比大多数语言都要慢,仅比 Ruby, PHP, JavaScript, Perl, TCL, Rebol 快一些。这次测试的两个特点是:递归,和创建大量小对象(约3000万),EF在这两方面似乎表现不佳。
(这次VC6的表现也很不好。)
还有很多测试题目,有时间再继续。想了解EF语言,请到EF官方博客:http://blog.csdn.net/efdev/