Notifications
Clear all

[Closed] So "return" is no longer slow?

 j83

A while back on tech artists forums, someone posted that return is no longer slow.

So this little test seems to show that. Has anyone else tried this? The reason I’d like to use return is that I find the resultant script a little cleaner, and easier to read.


    (
  	fn AddTwoNumbersV1 numberA numberB =
  	(
  		return (numberA + numberB)		
  	)
  	
  	fn AddTwoNumbersV2 numberA numberB =
  	(
  		(numberA + numberB)		
  	)
  	
  	for i = 1 to 3 do
  	(
  		format "
===============Round %===============
" i as string
  		local timeValueA = timeStamp()
  		for j = 1 to 500000 do
  		(
  			AddTwoNumbersV1 1 j
  		)
  		format "Time with return: %
" (timeStamp() - timeValueA)
 
   		
  		local timeValueB = timeStamp()
  		for k = 1 to 500000 do
  		(
  			AddTwoNumbersV2 1 k
  		)
  		format "Time without return: %
" (timeStamp() - timeValueB)
  		
  		gc()
  	)
  )
    

One test run,


  [b]===============Round 1===============[/b]
 [b] Time with return: 721[/b]
 [b] Time without return: 728[/b]
  
 [b] ===============Round 2===============[/b]
 [b] Time with return: 732[/b]
 [b] Time without return: 737[/b]
  
 [b] ===============Round 3===============[/b]
 [b] Time with return: 735[/b]
 [b] Time without return: 741[/b]
  
3 Replies

Hi,

That’s not exactly true. Return is slower when it is used to break the normal execution, when it is the last command in the function, it doesn’t slow it down.
Check out this modified version of your code for an example:


(
	  fn AddTwoNumbersV1 numberA numberB =
	  (
		  if numberA > 1 then
			return()
		
		return (numberA + numberB)		
	  )
	  
	  fn AddTwoNumbersV2 numberA numberB =
	  (
		  if numberA > 1 then ()
		  return (numberA + numberB)		
	  )
	  
	local repeats = 50000
	
	  for n = 1 to 3 do
	  (
		for i = 1 to 2 do (
			format "
===============Round %-%===============
" n i
			local timeValueA = timeStamp()
			for j = 1 to repeats do
			(
				AddTwoNumbersV1 i j
			)
			format "Time with return: %
" (timeStamp() - timeValueA)
	 
			
			local timeValueB = timeStamp()
			for k = 1 to repeats do
			(
				AddTwoNumbersV2 i k
			)
			format "Time without return: %
" (timeStamp() - timeValueB)
			
			gc()
		)
	  )
)

Cheers,
o

 j83

That’s fine, as I wouldn’t use it that way. I guess my main use of it would be just using it at the end of functions, just for code-cleanness.

i think is better to never use RETURN (CONTINUE, EXIT, and BREAK). That will make the real code-cleanliness

here is a sample of return using:


fn compare1 a b = (if a < b then a else b)
fn compare2 a b = (if a < b then return a else return b)
fn compare3 a b = (if a < b then return a; return b)

the functions 1 and 2 have exactly the same performance. the function 3 is much slower.