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:
over
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...
over
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:
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
P.S. you had a typo in your code you can't assign a number to "int". I think you meant int i = 4 ------------------ |
||||
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. ------------------ |
||||
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. ------------------ |
||||
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. ------------------ [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? ------------------ |
||||
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). @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: there's really nothing I disagree with there. however, would there be a real difference between using the > and using the =0? ------------------ |
||||
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. ------------------ |
||||
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: I know this may not be constructive, but is optimization really necessary? After all one of the rules of UNIX programming is: quote: More in-depth: quote: 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 ------------------ [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: 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 ------------------ |
||||
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 ![]() ![]() The design of your code is the most important part of it and for those just starting out, you'll learn |
||||
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. ------------------ [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 ![]() |
||||
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 ![]() |
||||
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. ------------------ |
||||
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. ------------------ [This message has been edited by samw3 (edited January 29, 2007).] |
||||
CPUFreak91![]() Member Posts: 2337 From: Registered: 02-01-2005 |
quote: Ok. So I shouldn't be so subjective on optimization. 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. I suppose that would matter for a web server or some web-based server-side code? I like that idea. Yeah. 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. But computers are getting so much faster that the slowdown is not much of an issue for most programs. ------------------ |
||||
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: 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. ------------------ |
||||
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: 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/ ------------------ [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 ![]() ![]() [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. ------------------ |
||||
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+). |