1. Type atom faster than type integer?
- Posted by lib9 2 weeks ago
- 207 views
Hi,
By logic and by what I think I've read in Euphoria documentation, a more restrictive type should be used whenever possible.
I would presume that a more restrictive type (like integer) would enable faster executions of code, instead of a more loose type (like atom).
atom t0 integer n sequence b_lst sequence prime_lst n = 100000000 b_lst = repeat( 1, n ) prime_lst = {} t0 = time() for prime = 2 to n do if b_lst[prime] then prime_lst = append(prime_lst, prime ) for i = prime * 2 to n by prime do b_lst[i] = 0 end for end if end for ? time() - t0
Running this version using integer n results in 6.060000002 seconds of runtime.
If that declaration is changed to atom n, the execution time drops a bit to 6.050000001 seconds.
Why does this happen?
2. Re: Type atom faster than type integer?
- Posted by achury 2 weeks ago
- 197 views
As I understand. On the internal interpreter data storage, integer and atom are the same.
The difference is that integer has type checking anytime a value is assigned, this may cause such delay.
3. Re: Type atom faster than type integer?
- Posted by DerekParnell (admin) 2 weeks ago
- 189 views
I'm sure the variation you are seeing is just an artifact of your machine's load during testing. Try running each variant (atom/integer) multiple times (e.g. 10) to get an average duration. The integer should be marginally faster, but the difference is usually negligible.
From memory, when using ATOM, Euphoria stores the value as an integer if it can and uses floating-point storage only if needed.
Here is some code that highlights the speed difference.
atom t0 integer n atom m t0 = time() for x = 1 to 5 do n = 0 for y = 1 to 100_000_000 do n += 1 end for end for ? time() - t0 t0 = time() for x = 1 to 5 do m = 0 for y = 1 to 100_000_000 do m += 0.11 end for end for ? time() - t0

