lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 20/12/2011 7:23 PM, Ico wrote:
Below I attached to small programs simply calculating sine tables, one Lua
version and one in C with - as far as I can see - the exact same
implementation.

The obvious difference is the print() function vs. printf(). I would expect these to be the most expensive lines of code seems plausible that you could see that kind of difference if the flushing behavior to stdout was different for example.

What are your results if you comment these out?

Ross



I have some problems explaining the difference in running time:

plain lua:            4.061s
luajit 2.0.0 beta 8:  0.076s
gcc -O3:              1.480s

Can onyone shed some light on why the C implementation is more then 20 (!) times
slower then the luajit version ?

I'm testing on a Intel Core 2 Duo CPU @ 2.20GHz. Could this be some kind of
architectural issue ?

-- test.lua ----------------------------------------------------------

local N = 4000
local S = 1000

local t = {}

for i = 0, N do
	t[i] = {
		a = 0,
		b = 1,
		f = i * 0.25
	}
end

for j = 0, S-1 do
	for i = 0, N-1 do
		t[i].a = t[i].a + t[i].b * t[i].f
		t[i].b = t[i].b - t[i].a * t[i].f
	end
	print(string.format("%.6f", t[1].a))
end


-- test.c ------------------------------------------------------------

#include<stdio.h>

#define N 4000
#define S 1000

struct t {
	double a, b, f;
};

int main(int argc, char **argv)
{
	int i, j;
	struct t t[N];

	for(i=0; i<N; i++) {
		t[i].a = 0;
		t[i].b = 1;
		t[i].f = i * 0.25;
	};

	for(j=0; j<S; j++) {
		for(i=0; i<N; i++) {
			t[i].a += t[i].b * t[i].f;
			t[i].b -= t[i].a * t[i].f;
		}
		printf("%.6f\n", t[1].a);
	}

	return 0;
}