General Development

Code Optimization – jestermax

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
Hey there, i was just thinking about something with regards to code optimization. When you compare values in code, it's actually more efficient to compare it to 0:

for( int = 4; i != 0; i-- ){..}

over

for( int = 0; i < 5; i++ ){..}

and if incrementing variables such as i++, it's more efficient to use ++i.

Of course this may differ on some compilers as each may have a different optimization algorithm, and some compilers may add in changes like switching i++ with ++i.

I possibly remember reading something about a trick for comparing objects to null...


if( null == object ){..}

over

if( object == null){..}

can anyone varify this?

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Sort of. The reason for the optimization is how C deals with booleans.

In C, 0=false, non-zero = true, so, to get the optimization, do this:


for( int i = 4; i; i-- ){..}

The reason this work is because "i" will be true until it hits zero.

And this will be slightly faster because the prefix decrementor is a sliver faster that the suffix decrementor


for( int i = 4; i; --i ){..}

P.S. you had a typo in your code you can't assign a number to "int". I think you meant int i = 4

------------------
Sam Washburn

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
yeah, thats what i meant, i just threw that together. thats a good point about the boolean iteration control, i didn't think of that.
i more meant to varify about object comparisons to null though. And if theres language specifics for C++ or Java (real languages) then it'd be nice to hear about those too.

For example: the Java compiler now automatically uses a stringbuffer object when you concatenate string objects. Java used to do a ton of calculations if you didn't handle it on your own.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
I'm not sure about the comparison to null. Seems like a good optimizing compiler could boil that one out.

I guess you could write a test program both ways and profile it and see which is best.

------------------
Sam Washburn

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
that's true. maybe i'll do that when i get home tonight. i was just thinking about the game engine i was working on and started wondering if i could optimize the sprite containers because theres a lot of iteration and operations on them.

Also, has anyone ever clocked the use of register variables in for loops? i know register variables aren't a "for sure" thing but i was wonering about averages.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
I've heard is not best to guess what the compiler optimizes, but to profile instead since that gives you the real performance metrics. So, that's what I do. I try to optimize after the code is written and only the parts where the profiler says its slow. That's just a personal practice.. I'm not sure if its a best practice.

------------------
Sam Washburn

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
yeah, it is a good practice. make it work, make it right, make it fast.
i just normally don't profile my code which i really want to start doing. i started thinking about it when i upgraded versions of jdeveloper and saw that i was updating the AI in one of my games twice, lol
SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
The 'null == object' thing may not be an optimization thing. It's just a helpful way to prevent a bad 'if' from occurring. The problem is, when going fast, you may only type one '=' sign.

if (object = null) will successfully set your object pointer to null.

if (null = object) will not compile.

So you want to put your null first in the comparison, so if you leave off an '=', your code will not compile and immediately alert you to the mistake.

I'll admit, I do not follow this rule. I still feel more comfortable comparing my known item to a value. I am thinking, "does my item equal such-and-such" as opposed to "does such-and-such equal my item".

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
I personally wouldn't do that, in the case that that you have something that decrements the i in the for loop, in that case, you could skip the 0 altogether and be stuck in an infinite loop.

of course, I forgot virtually all of C, so I'm going on the knowledge that you said a non-zero is true (including negatives)

I guess you can always watch for these in code, but I'm personally paranoid when it comes to this and would rather have a safty.
and frankly, with todays computers, the difference between a > and a =0 is negligible.
that's really more for the age when we used to code in assembly.
(in a note that none of you would care, I'm taking both a C and Assembly class this semester, so my ignorance shall be partially cured)

------------------
Realm Master: "I'm cockasian"
Soterion Studios

[This message has been edited by ArchAngel (edited January 26, 2007).]

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
Hey i never had a problem with a code being too slow ,how big of a deal is the speed of your code(i mean the code in a for loop)?

and also i have a question which one is better to use an integer as a flag in your code or a boolean?

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
For some companies/people, optimization is extremely important. For others, it doesn't matter. I fall into the 2nd category.

Is your flag/boolean question in regards to optimization or just asking in general. In general, when creating a new function/method, it is usually a good idea to use a flag. This lets you make updates without changing the interface. Especially important if you have exposed something to customers. But also helps so you don't personally have to rip through all of your code to make each change. It will, of course, most likely require code changes to take advantage of any newly added flags.

As for optimization, I have no clue.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
if you're talking about a flag to say if you found something in a loop or if a block of code calculates a yes or no answer then use the boolean. you don't need an integer for that and if someone else checks out your code then they won't know your value scheme.

As for optimization, a boolean is just a bit, (which is stored as a byte most of the time.. tsk tsk such a waste). an integer is 4 bytes (a common Int32).
so for a yes or no answer you'd be taking up 4 times as much space as if you were using a boolean.

@arch: generally i think it's bad practice to be changing loop variables.

As for my interest in optimization. If you're working on real-time programs (aka a game) then you want it to run as fast as possible. Just because it runs on your smokin' hot development pc doesn't mean it'll run on someone's Pentium 3. Anyways, thats just one of many reasons for optimization. Overall its just a better practice to write fast code and employers generally like that

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
quote:
@arch: generally i think it's bad practice to be changing loop variables.

As for my interest in optimization. If you're working on real-time programs (aka a game) then you want it to run as fast as possible. Just because it runs on your smokin' hot development pc doesn't mean it'll run on someone's Pentium 3. Anyways, thats just one of many reasons for optimization. Overall its just a better practice to write fast code and employers generally like that



there's really nothing I disagree with there.

however, would there be a real difference between using the > and using the =0?
isn't the real thing for optimization in the amount of looping you do, and stuff using O(n) and stuff?

------------------
Realm Master: "I'm cockasian"
Soterion Studios

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
a lot of optimization techniques are pointless if you're writing short programs or programs where time doesn't really matter.
if you're processing a few million array elements, etc then the small things really DO matter.
on the flip side, sometimes optimizing code too much can make it unreadable or more difficult to follow. i don't support that, odds are someone will have to read your code at some point, even if it's just you.
ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
yeah. we're on the same page here.
I remember having to write a program that created around 10k binary trees, each with 15k nodes... changing one little thing around cut the computation time in half. the biggest thing really is keeping the number of loops your computer is doing down to a minimum.

and yes, code should always be readable.

------------------
Realm Master: "I'm cockasian"
Soterion Studios

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
Arch made me realize...Let me take back, or at least clarify, what I said. I do believe in optimization. Speeding up graphics code. Speeding up computations. But the smaller, compiler optimization stuff is definitely not my type of thing.

Although I currently work in a semi-real time environment, we have not had to get to that level of detail. Our output, so far, is fast enough within the necessary time frame.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
actually, talking to dartsman reminded me of when i skimmed through the code for the game Jagged Alliance 2 (amazing game, one of my all times). it was C code with embedded assembler.
heres another project to check out: can embedded assember code be used to optimize processes?
CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by jestermax:
Hey there, i was just thinking about something with regards to code optimization. When you compare values in code, it's actually more efficient to compare it to 0:



I know this may not be constructive, but is optimization really necessary? After all one of the rules of UNIX programming is:
quote:
Rule of Optimization: Prototype before polishing. Get it working before you optimize it.

More in-depth:

quote:
“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil”. (C. A. R. Hoare)

I tend to follow these philosophies because I haven't had a need for optimization (except in Bible Dave) as my computer is so fast that it doesn't matter.

EDITs: Fixed formatting

------------------
All Your Base Are Belong To Us!!! chown -r us ./base
"After three days without programming, life becomes meaningless.'' -- Tao of Programming Book 2

[This message has been edited by CPUFreak91 (edited January 27, 2007).]

[This message has been edited by CPUFreak91 (edited January 27, 2007).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
well i'm not necessarily saying to optimize right away. that while sometimes it is a dumb idea to be thinking of optimization right off the bat, sometimes it's a good idea to keep it in mind.
But really, i don't understand your post...you said you weren't sure if optimization was necessary but then your quotes just stated that premature optimization is bad... care to clarify?
CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by jestermax:
you said you weren't sure if optimization was necessary but then your quotes just stated that premature optimization is bad... care to clarify?


I'm not sure if optimizing your code is necessary, as you would not notice a speed increase if you did optimize it (which, I am sure you are aware of). This is what has made interpreted languages so popular: The speed difference isn't really noticible.

My quotes are trying to start a discussion on premature optimization (and I wasn't entirely sure if you were going to let performance considerations affect the design of your code. (I see that I am wrong). You are right, this did need some clarification... and maybe I should go to bed .

------------------
All Your Base Are Belong To Us!!! chown -r us ./base
"After three days without programming, life becomes meaningless.'' -- Tao of Programming Book 2

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
that's true, against you won't notice most optimization unless you're doing a ton of iterations. i made a script for a company in Java (a Java script, not JavaScript ) that recursively searched a base directory on a linux server and switched their old name to their new one. i learned fairly quickly that it was better to try and optimize it as it took 10 minutes for the script to run in log mode (which allowed us to see what was happening and thus we needed to use log mode). Anyways, my point is, i just wanted to clarify that noone is really wrong here .

The design of your code is the most important part of it and for those just starting out, you'll learn . i'm a bit of a design junkie now; i gave presentations in college about design patterns and development methodologies . Not that i'm boasting knowledge here, i'm just saying its a blast to learn the stuff and its really, really useful to know.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Personally I have found that optimizations (both memory and speed) are inevitable in web app development because, if your site succeeds, as some point you will saturate the app and optimizing saves from having to build new app servers.

While I agree that premature optimization is evil(figuratively) I also have personally put into practice the post-optimization lessons previously learned in to new code.

I guess what I am saying is stop writing sloppy code and go ahead and optimize ahead of time as long as you a) don't waste your employer's/your own time optimizing things that really don't need to be and b) your code remains readable.

But, that's just my humble opinion and personal practice.

------------------
Sam Washburn

[This message has been edited by samw3 (edited January 27, 2007).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
very good point sam . experence lets you write good code from scratch instead of always having to spend time refactoring later. with agile programming, you're refactoring your code all the time so the quality is always getting better as the project progresses
HeardTheWord

Member

Posts: 224
From: Des Moines, IA
Registered: 08-16-2004
I agree with samw3 and jestermax. Optimization is not as important as writing understandable code that can be tweak later. One thing that helps prevent sloppy code is clearly analyzing the problem before diving into the code. Make sure you understand the problem before creating the solution.
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
yeah, vision and understanding is all important in our business (kinda like real life, lol). you need to make a solution to a problem, not make a solution and make it fit.
CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
Those of you who program in interpreted languages, how often to you optimize your code (when you see that it is running too slowly) and how many of you pre-optimize your code?

I use the faster modules in Python (such as cPickle instead of Pickle) and programming methods when there are two options to choose from, as long as they aren't so complex that they appear cryptic, but I don't really pre-optimize my code. If I discover that the code is a noticible resource hog, then I will optimize it.

------------------
All Your Base Are Belong To Us!!! chown -r us ./base
"After three days without programming, life becomes meaningless.'' -- Tao of Programming Book 2

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Well, asking whether you pre-optimize your code is kind of a subjective question. Sometimes a coder just produces "optimized" code as a nature of their experience. I think it comes down to the amount of time you spend on it and how crazy you get about it. You know? Like if you are so nit-picky that you spend half a day shaving cycles out of a loop where the time difference is in milliseconds.

I do believe that after a code base has become relatively stable (v1.0?) to do an optimize version even if there doesn't appear to be any slowdowns on your machine. Again, it a personal practice though, because I don't like bloated code. Another good reason is that others may not have as good a computer as your development box. But, then again, if you code on a 333Mhz and it runs fine, odds are you already have optimized it without realizing.

I've also learned not to trust my best judgment of what needs to be optimized. Profile, profile, profile.

As for interpreted langs, they will suffer the most from slowdowns because of the nature of interpretation, but even compiled code can slow down especially if it is server based. More connections mean more CPU cycles.

------------------
Sam Washburn

[This message has been edited by samw3 (edited January 29, 2007).]

CPUFreak91

Member

Posts: 2337
From:
Registered: 02-01-2005
quote:
Originally posted by samw3:
Well, asking whether you pre-optimize your code is kind of a subjective question.


Ok. So I shouldn't be so subjective on optimization.

quote:
I think it comes down to the amount of time you spend on it and how crazy you get about it. You know?


Ah. That makes sense. If you want your code to run fast on old computers you have to put some effort into optimizing it. If you're not that crazy about getting your code to run on everything under the sun (or, well before 1997) you want to spend more time on it.

quote:
Like if you are so nit-picky that you spend half a day shaving cycles out of a loop where the time difference is in milliseconds.


I suppose that would matter for a web server or some web-based server-side code?

quote:
I do believe that after a code base has become relatively stable (v1.0?) to do an optimize version even if there doesn't appear to be any slowdowns on your machine.


I like that idea.

quote:
Another good reason is that others may not have as good a computer as your development box.


Yeah.

quote:
But, then again, if you code on a 333Mhz and it runs fine, odds are you already have optimized it without realizing.

I've also learned not to trust my best judgment of what needs to be optimized. Profile, profile, profile.



I never really took profiling into much consideration. Maybe I should.

quote:
As for interpreted langs, they will suffer the most from slowdowns because of the nature of interpretation


But computers are getting so much faster that the slowdown is not much of an issue for most programs.

------------------
All Your Base Are Belong To Us!!! chown -r us ./base
"After three days without programming, life becomes meaningless.'' -- Tao of Programming Book 2

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
Plus - at least with Python, you can run speed boosters like Psyco on the source to make your program run faster.
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
ah, heres an update to this topic:
does anyone have any tricks on optimizing the visual studio 2005 compiler?
compiling C++ code is just plain slower then compiling C code (like 5-10 times more) but VS2k5 seems to slow it down even more
steveth45

Member

Posts: 536
From: Eugene, OR, USA
Registered: 08-10-2005
quote:
Originally posted by jestermax:
compiling C++ code is just plain slower then compiling C code (like 5-10 times more) but VS2k5 seems to slow it down even more

Well, C++ was made for speedy runtime, not compile time. In fact, writing a C++ parser is apparently extremely difficult due to the complexity of the syntax. The compiler with VS2k5 is much more ANSI compliant than 2k3, and is apparently better at catching problems during compilation. All this means slower compilation. You can speed things up a little by using precompiled headers.

------------------
+---------+
|steveth45|
+---------+

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
yeah, thats what i thought. someone suggested using forward declarations as well but i think its pretty futile.

on a side topic, i kinda want to write a c compiler. that could be a lot of fun

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
quote:
Originally posted by jestermax:
i kinda want to write a c compiler. that could be a lot of fun

Been there, done that. The hard part is finding someone to show it off to. With regular people you just get the deer-in-the-headlights look, and with other coders you get this why-in-the-world look.

But just in case I just dug up the ancient-and-yet-amazingly-still-applicable link.

http://compilers.iecc.com/crenshaw/

------------------
Sam Washburn

[This message has been edited by samw3 (edited February 03, 2007).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
YES!! great site , i'd better get started on this, lol. sadly it'll come after my other 2.75 projects though (because those are games )

[This message has been edited by jestermax (edited February 03, 2007).]

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
Regarding compilers, I never thought I'd have the need to write one. After taking the compiler courses in college, I was happy to be rid of the 'dragon' book.

...but you never know what happens. One day at work I was tasked with writing an expression analyzer in Java. Oh man was I scared. This was like the ultimate difficult task full of boredom. It actually ended up being quite fun and I still remember that task to this day. It really changed my perspective on the need to learn about compilers. And I've been able to use what I learned from that task in at least encouraging others who have had to do something similar.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
hmmm, after reading through parts of that tutorial, i've decided that i'm not big on pascal. anyone know of an online guide to writing compilers in c? a google search returns a bunch of stuff on "c compilers"
spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
i don't know about online but donald knuth is probably the most experienced compiler writer i know in the computer industry he has a book called The art of computer programming it has 4 volumes, i think there might be an ebook version available.

------------------
Matthew(22:36-40)"Teacher, which is the greatest commandment in the Law?" Jesus replied: " 'Love the Lord your God with all your heart and with all your soul and with all your mind. This is the first and greatest commandment. And the second is like it: 'Love your neighbor as yourself.All the Law and the Prophets hang on these two commandments."
Whose Son Is the Christ

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
*drools* that set looks amazing! i just checked out vol. 1-3 on amazing ($200+).