Page 1 of 1

Code optimization has given me a strange feeling!

Posted: Fri Apr 26, 2002 3:30 am
by Brian
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. :wink:

Posted: Fri Apr 26, 2002 7:07 am
by enygma
Memeory isn't something to be taken lightly in PHP programs either - I've seen a few of my scripts core dump either because of something silly I did or because an array I had in the file just got too dang big...

unset() is your friend....

Posted: Fri Apr 26, 2002 4:55 pm
by Jim
What does unset() do?

You guys are all confusing me...

Ya damn gurus.

Posted: Fri Apr 26, 2002 5:33 pm
by mydimension
unset() basiclly deallocates a variable from memory.
i.e.:
$var1 = 1; //variable is created and stored in memory

isset($var1); //returns true

unset($var1); deallocates variable from memory

isset($var1); //now returns false

hope that helps

Posted: Fri Apr 26, 2002 6:04 pm
by Jim
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 :)

Posted: Fri Apr 26, 2002 6:17 pm
by mydimension
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).

Posted: Fri Apr 26, 2002 6:37 pm
by Jim
How do you mark items as deleted?

Is there a special, simple code for it or is it a long, involved process?

Posted: Fri Apr 26, 2002 6:49 pm
by mydimension
the way i would do it is to create a field 'deleted' and insert and value of 1 into it when you delete that particular item

Re: Code optimization has given me a strange feeling!

Posted: Fri Apr 26, 2002 7:08 pm
by sam
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.
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.

Cheers Moe

I will have to disagree with your disagreement. ;)

Posted: Fri Apr 26, 2002 8:29 pm
by Brian
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.
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.

Code: Select all

Preprocessor Directive: #define PI 3.14
Memory Constant: const double pi = 3.14;

Posted: Fri Apr 26, 2002 11:07 pm
by jason
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:

Code: Select all

i = 32 * j;
This is compiled as:

Code: Select all

i = j << 5; /* or 2**5 == 32 */
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

Code: Select all

const int SIZE = 30;
You would want to change it to:

Code: Select all

const int SIZE = 32;
It would mean we waste some memory, but the program is faster.

Another consideration is something like this:

Code: Select all

for ( i=0; i<10; ++i )
    matrix&#1111;i] = i + j * 10;
While that is okay, you could write it like this:

Code: Select all

j_times_ten = j * 10;
for ( i=0; i<10; ++i )
    matrix&#1111;i] = i + j_times_ten;
This avoids doing the j * 10 over and over again.

These are just a few tricks I know of.

Posted: Sat Apr 27, 2002 2:45 pm
by sam
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

Preprocessor Directives

Posted: Sat Apr 27, 2002 5:03 pm
by Brian

Posted: Sun Apr 28, 2002 12:29 pm
by sam
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