0

I think this is actually a simple question for comp. sci. people, but I am not in a comp. sci. program and can't remember any terminology.

I have made a large set of functions which are essentially nested to create a large numpy record array.

data = func_to_make_large_structure()    

Now, when I assign data like this, does it store the large structure as it is after the function returns, or does the function get called and re-calculate every time it is called? It is taking 30 seconds to make the structure, which is considerably longer than it takes for a similar structure to be created in OO by using an object to store the data.
In addition, I then do a large amount of processing on the data which takes much longer for the functional program than the OO form.

Is this because the data is being recalculated every time the data is called? If so, how do I store just the data without calling the function everytime?

chase
  • 3,062
  • 6
  • 27
  • 53
  • 1
    It would probably be useful to tag this with the language you are using – beresfordt Feb 23 '15 at 23:36
  • Usually in these cases, referencing `data` after this line will not invoke another call to `func_to_make_large_structure()` because you are assigning the *result* of that function into `data`. HOWEVER, some languages allow you to pass back function pointers or function references. If that's the case, then you may be invoking a function call every time you reference `data`. So, as said above, it would be really helpful to know 1) your language 2) what the declaration of `func_to_make_large_structure()` looks like (what are the parameters and return types?) – wilkesybear Feb 23 '15 at 23:50

1 Answers1

1

After the function is called, the variable data no longer has any association with the function, so if you call func_to_make_large_structure() again, it will do all the same calculations again. A technique to avoid this is called memoization and techniques for using it in Python are described in this question.

But if you're asking if after the initial call, func_to_make_large_structure is called each time the data variable is accessed, then no.

Community
  • 1
  • 1
Andrew Magee
  • 5,913
  • 4
  • 31
  • 56