windows – 为什么在lisp中计算到10亿这么慢?
(defun billion-test () (setq i 0) (loop while (< i 100) do (setq i (+ i 1)))) (billion-test) (print "done") 我有上面的Lisp代码,简单地循环到十亿.问题是它确实存在 Compiled Uncompiled GNU Common Lisp(gcl) 270-300s 900-960s Clisp 280-300s 960-1647s 我使用这个Python代码来计算Clisp的时间并使用系统时间进行近似 import sys import time import os start=time.time() os.system(" ".join(sys.argv[1:])) stop=time.time() print "n%.4f secondsn"%(stop-start) 以下是与其他语言的while循环的比较: Kawa scheme 220.3350s Petite chez 112.827s C# 1.9130s Ruby 31.045s Python 116.8600s 113.7090s(optimized) C 2.8240s 0.0150s(optimized) lua 84.6970s 我的假设是循环而< condition>做的是Lisp相当于一段时间 更新1: compiled uncompiled gcl 0.8s 12mins clisp 5mins 18mins gcl编译程序很好,但clisp给出了这个警告: ;; Compiling file C:mine.cltest.cl ... WARNING: in BILLION-TEST in lines 1..8 : FIXNUM-SAFETY is not a valid OPTIMIZE quality. 0 errors,1 warning ;; Wrote file C:mine.cltest.fas ;; clisp [2]> (type-of 1000000000) (INTEGER (16777215)) ;;gcl (type-of 1000000000) FIXNUM 猜猜这可能是花了一分多钟的原因. 更新2: * (print most-positive-fixnum) 536870911 * (compile-file "count-to-billion.cl") ; compiling file "C:/mine/.cl/count-to-billion.cl" (written 09 OCT 2013 04:28:24 PM): ; compiling (DEFUN BILLION-TEST ...) ; file: C:/mine/.cl/count-to-billion.cl ; in: DEFUN BILLION-TEST ; (OPTIMIZE (SPEED 3) (SAFETY 0) (DEBUG 0) (FIXNUM-SAFETY 0)) ; ; caught WARNING: ; Ignoring unknown optimization quality FIXNUM-SAFETY in: ; (OPTIMIZE (SPEED 3) (SAFETY 0) (DEBUG 0) (FIXNUM-SAFETY 0)) * (load "count-to-billion") 我希望我可以告诉你需要多长时间,但我从来没有看到它的结束.我等了 用gcl运行R?rd的代码: (time (loop with i = 0 while (< i 1000000000) do (incf i))) gcl with Rords's code: >(load "count-to-billion.cl") Loading count-to-billion.cl real-time : 595.667 secs run time : 595.667 secs >(compile-file "count-to-billion.cl") OPTIMIZE levels: Safety=0 (No runtime error checking),Space=0,Speed=3 Finished compiling count-to-billion.cl. #p"count-to-billion.o" >(load "count-to-billion") Loading count-to-billion.o real time : 575.567 secs run time : 575.567 secs start address -T 1020e400 Finished loading count-to-billion.o 48 更新3: 这是最后一个,我保证.我试过Rords其他代码: (defun billion-test () (loop with i fixnum = 0 while (< i 1000000000) do (incf i))) 令人惊讶的是,它的运行速度与Joswig一样快,不同之处在于关键字fixnum和 gcl的输出: real time : 0.850 secs run time : 0.850 secs sbcl的输出(运行大约半秒钟然后吐出来): debugger invoked on a TYPE-ERROR in thread #<THREAD "main thread" RUNNING {23FC3A39}>: The value 536870912 is not of type FIXNUM. clisp的输出: Real time: 302.82532 sec. Run time: 286.35544 sec. Space: 11798673420 Bytes GC: 21413,GC time: 64.47521 sec. NIL
>启动时间
>未声明的变量 >全局变量 >没有类型声明 >编译器没有告诉优化 >在32位机器/实现上1000000000可能不是fixnum,请参见变量MOST-POSITIVE-FIXNUM >可能<与32位机器上的bignum比较 - >最好数到0 >执行缓慢 64位Common Lisp应该有更大的fixnums,我们可以使用简单的fixnum计算. 在配备2 Ghz Intel i7的MacBook Air笔记本电脑上的64位LispWorks上,我获得了未经优化的代码,可在2秒内完成.如果我们添加声明,它会更快一些. (defun billion-test () (let ((i 0)) (declare (fixnum i) (optimize (speed 3) (safety 0) (debug 0)) (inline +)) (loop while (< i 1000000000) do (setq i (+ i 1))))) CL-USER 7 > (time (billion-test)) Timing the evaluation of (BILLION-TEST) User time = 0.973 System time = 0.002 Elapsed time = 0.958 Allocation = 154384 bytes 0 Page faults NIL 64位SBCL需要0.3秒.所以它更快. 使用GCL,您应该能够在32位机器上获得更好的结果.在这里,我在32位ARM处理器(Samsung Exynos 5410)上使用GCL.在ARM机器上使用GCL的十亿仍然是一个固定的. >(type-of 1000000000) FIXNUM >(defun billion-test () (let ((i 0)) (declare (fixnum i) (optimize (speed 3) (safety 0) (debug 0)) (inline +)) (loop while (< i 1000000000) do (setq i (+ i 1))))) BILLION-TEST >(compile *) Compiling /tmp/gazonk_23351_0.lsp. Warning: The OPTIMIZE quality DEBUG is unknown. End of Pass 1. End of Pass 2. OPTIMIZE levels: Safety=0 (No runtime error checking),Speed=3 Finished compiling /tmp/gazonk_23351_0.lsp. Loading /tmp/gazonk_23351_0.o start address -T 0x7a36f0 Finished loading /tmp/gazonk_23351_0.o #<compiled-function BILLION-TEST> NIL NIL 现在您可以看到GCL也非常快,即使在较慢的ARM处理器上: >(time (billion-test)) real time : 0.639 secs run-gbc time : 0.639 secs child run time : 0.000 secs gbc time : 0.000 secs NIL (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Win10系统配置Java环境变量
- win10系统上Python和pycharm的安装及配置
- Windows Server 2008 WebDAV文件夹上的Mac WebDA
- windows-server-2003 – 如何在不提示管理员密码
- Microsoft安全通报(2269637)对.NET编码的影响
- windows-7 – 在程序关闭时用批处理文件更改壁纸
- windows-phone-7 – 如何以编程方式检查Windows设
- windows – 在VMWare Workstation 7.1.0中输入虚
- Windows平台的原始套接字编程的知识点概要(备忘
- windows-server-2008 – 向Jr. SysAdmin解释当PC