1. Type atom faster than type integer?

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?

new topic     » topic index » view message » categorize

2. Re: Type atom faster than type integer?

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Type atom faster than type integer?

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 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu