Code optimization has given me a strange feeling!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Code optimization has given me a strange feeling!

Post 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:
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post 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....
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

What does unset() do?

You guys are all confusing me...

Ya damn gurus.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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 :)
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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).
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

How do you mark items as deleted?

Is there a special, simple code for it or is it a long, involved process?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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
User avatar
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!

Post 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
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

I will have to disagree with your disagreement. ;)

Post 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;
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post 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
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Preprocessor Directives

Post by Brian »

User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post 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
Post Reply