Last active
December 31, 2015 16:29
-
-
Save klaufir/8014179 to your computer and use it in GitHub Desktop.
Runtime-eval tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; SBCL 1.1.12, limit = 10M, 20M, 40M | |
; 22:37 <fikusz> result: 3.7037042592681206d19 | |
; 22:37 <fikusz> ./lispeval.exe 0.44s user 0.01s system 99% cpu 0.456 total | |
; 22:38 <fikusz> result: 2.9629631851909926d20 | |
; 22:38 <fikusz> ./lispeval.exe 0.85s user 0.01s system 99% cpu 0.867 total | |
; 22:38 <fikusz> result: 2.3703704592622897d21 | |
; 22:38 <fikusz> ./lispeval.exe 1.64s user 0.02s system 99% cpu 1.672 total | |
(defun main-fn () | |
(declare (optimize (speed 3) | |
(space 0) | |
(compilation-speed 0) | |
(safety 0) | |
(debug 0))) | |
(let ((input "(/ (+ (/ (* x x) 3.0d0) 2.0d0) 3.0d0)")) | |
(setf (symbol-function 'test-fn) | |
(compile nil | |
`(lambda (x) | |
(declare (type double-float x)) | |
(declare (inline dispatch)) | |
(declare (optimize (speed 3) | |
(space 0) | |
(compilation-speed 0) | |
(safety 0) | |
(debug 0))) | |
(the double-float ,(read-from-string input)))))) | |
(format t (concatenate | |
'string | |
"result: " | |
(write-to-string | |
(loop for i from 1.0d0 to 10000000.0d0 | |
sum (test-fn i))) | |
"~%"))) | |
(sb-ext:save-lisp-and-die | |
"lispeval.exe" | |
:executable t | |
:toplevel 'main-fn) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--LuaJIT 2.1, limit = 10M, 20M, 40M | |
--22:59 <fikusz> 3.7037042592681e+19 | |
--22:59 <fikusz> ./luajit rteval.lua 0.18s user 0.00s system 98% cpu 0.179 total | |
--23:00 <fikusz> 2.962963185191e+20 | |
--23:00 <fikusz> ./luajit rteval.lua 0.31s user 0.00s system 97% cpu 0.318 total | |
--23:00 <fikusz> 2.3703704592623e+21 | |
--23:00 <fikusz> ./luajit rteval.lua 0.56s user 0.00s system 98% cpu 0.563 total | |
function split(s, delimiter) | |
result = {} | |
for match in (s..delimiter):gmatch("(.-)"..delimiter) do | |
table.insert(result, match) | |
end | |
return result | |
end | |
function feval(expr) | |
local params, body = unpack(split(expr,"->")) | |
return loadstring("return function(" .. params ..") return ".. body .." end")() | |
end | |
-- feval ends | |
do | |
funs = {} | |
funs["mul"] = function(x,y) return x*y end | |
funs["div"] = function(x,y) return x/y end | |
funs["add"] = function(x,y) return x+y end | |
end | |
function egen(expr) | |
local r = "" | |
for i,v in ipairs(expr) do | |
if i == 1 then r='funs["'..tostring(v)..'"]( ' | |
elseif type(v) == "table" then r=r..egen(v) | |
else r=r..tostring(v) end | |
if #expr ~= i and 1 ~= i then r=r..", " end | |
end | |
r=r..")" | |
return r | |
end | |
e = {"div",{"add",{"div",{"mul","x","x"},3},2},3} | |
lua_test = feval("x->"..egen(e)) | |
function test() | |
local sum = 0 | |
for i=1,10000000 do | |
sum = sum + lua_test(i) | |
end | |
return sum | |
end | |
sum=test() | |
print(sum) | |
--generated machine code | |
--7efafeb0 xorps xmm6, xmm6 ;xmm6 = 0 | |
--7efafeb3 cvtsi2sd xmm6, edi ;xmm6 = (double) edi | |
--7efafeb7 mulsd xmm6, xmm6 ;xmm6 *= xmm6 | |
--7efafebb divsd xmm6, xmm0 ;xmm6 /= xmm0 | |
--7efafebf addsd xmm6, xmm1 ;xmm6 += xmm1 | |
--7efafec3 divsd xmm6, xmm0 ;xmm6 /= xmm0 | |
--7efafec7 addsd xmm7, xmm6 ;xmm7 += xmm6 | |
--7efafecb add edi, +0x01 ;edi+=1 | |
--7efafece cmp edi, 0x00989680 ;cmp edi, 10000000 | |
--7efafed4 jle 0x7efafeb0 ->LOOP ;if edi <= 10000000, goto LOOP | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
GCC 4.8.2, limit = 10M, 20M, 40M | |
23:19 <fikusz> 3.703704e+19 | |
23:19 <fikusz> ./static1.out 0.20s user 0.00s system 98% cpu 0.203 total | |
23:20 <fikusz> 2.962963e+20 | |
23:20 <fikusz> ./static1.out 0.37s user 0.00s system 99% cpu 0.376 total | |
23:21 <fikusz> 2.370370e+21 | |
23:21 <fikusz> ./static1.out 0.72s user 0.00s system 99% cpu 0.721 total | |
*/ | |
// static test version | |
// gcc -O3 static1.c -o static1 | |
#include <stdio.h> | |
double add(double x, double y) { return x+y; } | |
double mul(double x, double y) { return x*y; } | |
double div(double x, double y) { return x/y; } | |
double test(double x) | |
{ | |
return div(add(div(mul(x,x),3.0),2.0),3.0); | |
} | |
int main() | |
{ | |
double sum = 0.0; | |
int i; | |
for(i=1;i <= 10000000.0;++i) | |
sum = sum + test(i); | |
printf("%e\n", sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment