[Gambas-user] Interesting performance optimization

Brian G brian at westwoodsvcs.com
Sat Dec 4 05:02:24 CET 2021


The is a remarkable difference in the performance of the following functions. 
Without fast unsafe options the second is 100% or more slower than the first. 

With fast unsafe options the second is 8 to 10 percent faster then the first, the older or slower the machine the better the performance of the second compared to the first. 

On newer faster machines the difference is negligible. This really makes you think about how to optimize your code in a targeted environment! 

sample 1) 
sub smaller(a as long, b as long) as long 
if a < b then return a else return b 
end 

Sample 2) 
sub small(a as long, b as long) as long 
return (a * (1 + (a > b))) + ( b * (1 + (b >= a))) ' inverse of the operation because true is -1 false is 0 
end 

Sample Script follows: 

#!/usr/bin/gbs3 
' Gambas Script File Created 12/04/2021 02:39:16.184 
fast unsafe 
print smaller(992,100) 
Print small(992,100) 
Print Smaller(1,100) 
Print small(1,100) 

dim ttime as float = timer 
for i as long = 0 to 1000000000 
smaller(i,5000000) 
next 
print timer - ttime 

ttime = timer 
for i as long = 0 to 1000000000 
small(i,5000000) 
next 
print timer - ttime 

catch 
Print error.text&"\n" & error.where 

sub smaller(a as long, b as long) as long 
if a < b then return a else return b 
end 

sub small(a as long, b as long) as long 
return (a * (1 + (a > b))) + ( b * (1 + (b >= a))) 
end 


"Failure is the key to success; 
each mistake teaches us something" .. Morihei Ueshiba 
Brian G 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20211203/4d0dab39/attachment.htm>


More information about the User mailing list