compiler construction - Using a variable to store a function call? -


let's had pure function takes considerable amount of time run. , in main wanted call function same arguments multiple times.

my ultimate question is, should store return value in variable , use or call function multiple times? way take less computations?

are compilers modern languages (if any) able tell whether function pure or not? if yes, compilers able optimize away multiple calls in same block? if yes, makes more sense me call functions use placeholder variable (since wasting computation doing assignment/binding names)?

edit: here example

if mypurefunction(a,b) == 1:     print(1) elif mypurefunction(a,b) == 2:     print(2) elif mypurefunction(a,b) == 3:     print(3) else:     print(4) 

vs.

var = mypurefunction(a,b)  if var == 1:     print(1) elif var == 2:     print(2) elif var == 3:     print(3) else:     print(4) 

thanks in advance.

your answer depends optimization of compiler. if body of function 'mypurefunction()' in same translation unit (your c-file), compilers can perform optimization first example , replace 3x calls of function one. not compilers can make optimization , second variant better. said it, because our compiler (that, implemented in work) can't =)


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -