I feel strange now... as though by merely considering such optimizations I have somehow betrayed the principles of Perl and PHP programming. Heh.
Code optimization has given me a strange feeling!
Moderator: General Moderators
Code optimization has given me a strange feeling!
There I was the other day, sitting in my C class and listening to the instructor go on about how constants are better to use than variables in C programs because constants cannot be changed by errant coworker code when it occurred to me that constants declared as preprocessor directives should also result in a final program that requires less RAM. Not much less RAM, but still less RAM. 
I feel strange now... as though by merely considering such optimizations I have somehow betrayed the principles of Perl and PHP programming. Heh.
I feel strange now... as though by merely considering such optimizations I have somehow betrayed the principles of Perl and PHP programming. Heh.
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
So basically, it "deletes" the key from being read without actually removing it from memory?
In another topic I was asking what happens to information when stuff is deleted from the forums and I was told it was either deleted, or made to "seem" deleted. Is that what this is all about?
If so, what's the point of using it? Why not just delete it? For evidence or something that want kept out of sight but may need later?
Thanks much
In another topic I was asking what happens to information when stuff is deleted from the forums and I was told it was either deleted, or made to "seem" deleted. Is that what this is all about?
If so, what's the point of using it? Why not just delete it? For evidence or something that want kept out of sight but may need later?
Thanks much
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
no, it dosen't. when you define a variable it takes up a certain amount of memory. using unset() un-defines that variable and frees up that memory so that it can then be used for something else.
in regards to the other topic [the forum one] you can either delete the item from the database (completely destroyed and cannot be accessed again) or by choice you design you dBase to mark items as deleted (for archival purposes).
in regards to the other topic [the forum one] you can either delete the item from the database (completely destroyed and cannot be accessed again) or by choice you design you dBase to mark items as deleted (for archival purposes).
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Re: Code optimization has given me a strange feeling!
I will have to diss agree with you. The only way that they will use less ram is if they are smaller, When a program is launced the whole thing is loaded into ram, so if you defined a bunch of constants they will be in ram, if you defined a bunch of variables with constant data, they will be in ram. Sure constants will require a small bit less ram, but there is really no huge binifit. The way to greatly reduce the size of your program in ram is to use dynamicly linked libraries.Brian wrote:constants declared as preprocessor directives should also result in a final program that requires less RAM. Not much less RAM, but still less RAM.
Cheers Moe
I will have to disagree with your disagreement. ;)
If you define constants as preprocessor directives, they are replaced at compile time, so when the program is launched, they are only loaded into RAM as the various statements in the program, not as declared anything. Constants are not hanging around in RAM like variables unless you define them as memory constants.I will have to diss agree with you. The only way that they will use less ram is if they are smaller, When a program is launced the whole thing is loaded into ram, so if you defined a bunch of constants they will be in ram, if you defined a bunch of variables with constant data, they will be in ram.
Code: Select all
Preprocessor Directive: #define PI 3.14
Memory Constant: const double pi = 3.14;Well, Memory and Speed are not the same. Saving Memory doesn't mean you increase the speed of your program. In fact, in some cases, it's the opposite.
For example, in C:
This is compiled as:
Basically, what you are seeing is that in C, using multiples of 2 is a good thing. So if you define a variable such as
You would want to change it to:
It would mean we waste some memory, but the program is faster.
Another consideration is something like this:
While that is okay, you could write it like this:
This avoids doing the j * 10 over and over again.
These are just a few tricks I know of.
For example, in C:
Code: Select all
i = 32 * j;Code: Select all
i = j << 5; /* or 2**5 == 32 */Code: Select all
const int SIZE = 30;Code: Select all
const int SIZE = 32;Another consideration is something like this:
Code: Select all
for ( i=0; i<10; ++i )
matrixїi] = i + j * 10;Code: Select all
j_times_ten = j * 10;
for ( i=0; i<10; ++i )
matrixїi] = i + j_times_ten;These are just a few tricks I know of.
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
I would venture to say that the way const and define are handled is up to the compiler. Should you use one instead of the other hoping for memory gain and spped depends totally on the compiler that you are using.
So in order to find which one is more optimised you will have to venture into the debug state with your compiler.
Here are some articles on the subject...
http://www.embedded.com/story/OEG20011129S0065
http://cpptips.hyperformix.com/cpptips/const_vs_def
http://www.oualline.com/style/c06.html
Cheers Sam
So in order to find which one is more optimised you will have to venture into the debug state with your compiler.
Here are some articles on the subject...
http://www.embedded.com/story/OEG20011129S0065
http://cpptips.hyperformix.com/cpptips/const_vs_def
http://www.oualline.com/style/c06.html
Cheers Sam
Preprocessor Directives
By definition, preprocessor directives are instructions for the preprocessor, which is invoked at compile time, not for the resulting program.
Here is some related leisure reading:
http://usgibm.nersc.gov/vac/concepts/cuprdovr.htm
http://www.cs.cf.ac.uk/Dave/C/node14.html
http://www.le.ac.uk/cc/iss/tutorials/cp ... rt.html#PP
http://www.cl.cam.ac.uk/texinfodoc/cpp.html#SEC2
http://www.cplusplus.com/doc/tutorial/tut5-5.html
http://msdn.microsoft.com/library/defau ... ctives.asp
Here is some related leisure reading:
http://usgibm.nersc.gov/vac/concepts/cuprdovr.htm
http://www.cs.cf.ac.uk/Dave/C/node14.html
http://www.le.ac.uk/cc/iss/tutorials/cp ... rt.html#PP
http://www.cl.cam.ac.uk/texinfodoc/cpp.html#SEC2
http://www.cplusplus.com/doc/tutorial/tut5-5.html
http://msdn.microsoft.com/library/defau ... ctives.asp
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Yeah I know what a preprocessor directive is. Most of the newer c++ compilers will break the const doen into code that is comparable to a preprocessor directive so optimaising it to the point that using ether one might be me effient. The way a preprocessor directive is defined is also different for each compiler, I was just saying that choosing one over the other in all sercomstances isn't nessacarily going to make your program memory effient.
Cheers Sam
Cheers Sam