d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > C++ Memory Question > Pass A Parameter, Or Make A Calc?
Add Reply New Topic New Poll
Member
Posts: 2,315
Joined: Oct 22 2009
Gold: 43.50
Oct 27 2015 12:59pm
I am writing a program here that is abstracted into 3 methods. All methods require an h and rho value given by the user (both are doubles)

2/3 methods require a (h^2)*rho

Is it more memory efficient to:

A) Pass this values into the methods every time

B) Make them globals, set them in the main when the user enters the values.

C) With special thought on the h squared * rho, Should I calculate that once and pass/set as global or calculate it thousands of times inside the methods?
Member
Posts: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Oct 27 2015 03:53pm
It would be more memory efficient to store them as globals, but I wouldn't do that unless you are writing for an embedded system with memory being the absolute biggest concern. It makes for brittle code that's hard to debug.
If you are literally calculating the same value "thousands of times", then you should probably calculate once (or few times, if it makes sense) and pass it in. Hard to say which approach is more memory efficient, but the difference would be insignificant anyway. Power consumption would be drastically different though, which is often a bigger concern for embedded systems than memory.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2015 07:02pm
Don't worry about memory as much as design. Are these utility functions? or are you computing something specific? if so, just make them a class variable if that's what makes the most sense.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll