c# - memorizing values in variables VS using functions/methods that return values -



c# - memorizing values in variables VS using functions/methods that return values -

i have been wandering time now. i'm still beginner, programs simple , require no resources. i'm familiar c++, java , c#. in future things more complicated, more variables , objects , more functions/methods.

in order alter value of variable, can phone call function/method, changes value, once. after when need variable, have phone call it. takes less time value. if function lot of work, time has consumed experienced once. variables require space in memory , when quantity gets quite big, can become problem.

but can phone call function/method returns value need. need utilize variables necessary, means- less variables. it's not problem if function/method short. functions/methods must quite lot of work before homecoming value. way programme seem slower user.

my question is: memorizing values in variables vs using functions/methods homecoming value: better, when , why?

this question sounds like: high speed , more consumed memory vs less consumed memory , low speed...

it wise avoid caching values can computed, since can avoid having maintain state in separate places (and breaking consistency if forget).

it thought abstract away on calculation of value (like hiding behind function). later 1 time code works , sense slow, utilize profiling determine hotspots. if such function turns out hotspot, can introduce caching / memoizing underneath, , abstraction (in ideal world) cover nasty details client (of function).

memoization

memoizing function involves lookup table (based on array, associative array, etc.) key type beingness aggregate of arguments of memoized function, , stored value beingness homecoming value of function.

then, function first check if result available in lookup table , homecoming it, or else compute result , store before returning.

in fact, such memoization disjoined memoized function, , implemented separate layer (wrapping function) if programming language supports such abstraction. in languages without first-class functions, can achieved using interfaces , single-method classes.

c# java c++ function memory

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -