944,155 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1246
  • C RSS
May 23rd, 2006
0

Saving Memory vs Saving Processing Power

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006
May 23rd, 2006
0

Re: Saving Memory vs Saving Processing Power

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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
May 24th, 2006
0

Re: Saving Memory vs Saving Processing Power

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
May 24th, 2006
0

Re: Saving Memory vs Saving Processing Power

Good explaination, thanks!
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: send() problem again
Next Thread in C Forum Timeline: _snprintf question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC