Re: switch bug?

new topic     » goto parent     » topic index » view thread      » older message » newer message
euphoric said...

Is if...elsif...endif just as fast as the switch statement?

I was thinking that switch should be faster than if ... elsif ... end if, however this benchmark suggests that if ... elsif ... end if is faster. Matt, what are your thoughts?

integer a = 20 
atom t0 
 
t0 = time() 
for b = 1 to 100000000 do 
	switch a do 
		case 5: 
			a = 10 
			break 
		 
		case 10: 
			a = 15 
			break 
		 
		case 15: 
			a = 20 
			break 
		 
		case 20: 
			a = 25 
			break 
	 
		case 25: 
			a = 30 
			break 
		 
		case 30: 
			a = 35 
			break 
	 
		case else 
			a = 5 
	end switch 
end for 
 
printf(1, "switch=%f\n", {time()-t0}) 
 
t0 = time() 
for b = 1 to 100000000 do 
	if a = 5 then 
		a = 10 
	elsif a = 10 then 
		a = 15 
	elsif a = 15 then 
		a = 20 
	elsif a = 20 then 
		a = 25 
	elsif a = 25 then 
		a = 30 
	elsif a = 30 then 
		a = 35 
	else 
		a = 5 
	end if 
end for 
 
printf(1, "if=%f\n", {time()-t0}) 

When run the output is:

switch=2.730000 
if=2.465000 

Jeremy

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu