Saving Memory vs Saving Processing Power

Thread Solved

Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Saving Memory vs Saving Processing Power

 
0
  #1
May 23rd, 2006
disclaimer
I'm a programming n00b so I may not know what I'm talking about.
/disclaimer

My question boils down to scopes.
Let say I have code
  1. {
  2. string mystring1;
  3. string mystring2;
  4. string mystring3;
  5.  
  6. //code dealing with mystring1
  7.  
  8. //code dealing with mystring2
  9.  
  10. //code dealing with mystring3
  11. }

versus

  1. {
  2. string mystring1;
  3. //code dealing with mystring1
  4. }
  5.  
  6. {
  7. string mystring2;
  8. //code dealing with mystring2
  9. }
  10.  
  11. {
  12. string mystring3;
  13. //code dealing with mystring3
  14. }

Now my guess is that the first block of code uses more memory at any given moment than the second one, and that the second block of code uses more than or equal processing power than the first one.

Are these assumptions true? Which one would you use? And each mystring only has scope within the {} enclosing it right?

I ask this because I have some code that initializes like 10 strings at once (like block 1) and then works with them one by one disjointly from eachother. And when I run my program, it lags a second during the initialization of these strings. So I figured I could rewrite it like block 2 to make it run smoother.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Saving Memory vs Saving Processing Power

 
0
  #2
May 23rd, 2006
I think if possible you should go as local as you can keeping in mind memory.

As far as processing power is concerned, you should'nt care unless you are working on a system having limited amount of processing power. You can also profile your code to check.
I don't have any exact answer for this. Maybe someone else will through some more light on it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Saving Memory vs Saving Processing Power

 
0
  #3
May 24th, 2006
>>Now my guess is that the first block of code uses more memory at any given moment than the second one
In the second example, a good optimizing compiler will notice that it only needs to allocate space for one std::string object and reuse this memory on each block entry. All memory is allocated on the stack during function entry, not block entry. That makes the second example more memory efficient than the first. It would be the same as declaring one std::string object at the beginning of the function and re-using it in each block, something like this: (in this example you don't need the blocks at all!)
  1. int foo()
  2. {
  3. std::string str;
  4. {
  5. str = "Hello";
  6. // blabla
  7. }
  8. {
  9. str = "By";
  10. // blabla
  11. }
  12. }
>>And each mystring only has scope within the {} enclosing it right?
That is correct -- but it doesn't mean that the memory allocated for the objects are removed from the stack when the block terminates. It is only released when the function returns to whoever called it.

>>the second block of code uses more than or equal processing power than the first one
Depends on how you look at it. Overall, from the beginning to the end of the function they are both equal. But the second will consume less initial processing than the first because std::string class constructor will be called for all three strings at the same time in the first example, while the constructors are called only duirng block entry in the second example.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Saving Memory vs Saving Processing Power

 
0
  #4
May 24th, 2006
Good explaination, thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC