| | |
Saving Memory vs Saving Processing Power
Thread Solved |
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
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
versus
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.
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
C Syntax (Toggle Plain Text)
{ string mystring1; string mystring2; string mystring3; //code dealing with mystring1 //code dealing with mystring2 //code dealing with mystring3 }
versus
C Syntax (Toggle Plain Text)
{ string mystring1; //code dealing with mystring1 } { string mystring2; //code dealing with mystring2 } { string mystring3; //code dealing with mystring3 }
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.
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.
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.
>>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!)
>>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.
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!)
C Syntax (Toggle Plain Text)
int foo() { std::string str; { str = "Hello"; // blabla } { str = "By"; // blabla } }
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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Finding Perfect Numbers. "help plz" (C++)
- Boot issues.. the 3 day deadline (Troubleshooting Dead Machines)
- have you ever abused ur powers? (Geeks' Lounge)
- Slow Computer (Windows 95 / 98 / Me)
Other Threads in the C Forum
- Previous Thread: send() problem again
- Next Thread: _snprintf question
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






