|
there's no need to optimize code in C++. cauze the compiler optimizes the code much better than me and this way my code looks neat.
|
|
|
|
|
But you have to remember that there are things that the compiler simply cannot optimize; for example, unnecessary dynamic resource (memory) management.
There is lots of code out there that does unnecessary things, but the complier cannot help. For example, I have seen code similar to this in MSDN as a example (a very BAD one) of how to do custom drawing of text:
<br />
LOGFONT *pFL = new LOGFONT;<br />
delete pLF;<br />
(pLF was only used locally.) And I had seen that code being used to draw multiple lines of text, where pLF was being released and reallocated each time the format changed! (And how often is drawing code being called?)
Of course, there is no need to waste CPU time dynamically allocating the LOGFONT object; having one on the stack will work better, because no extra time is wasted with dynamic memory management.
That is one example of where the compiler cannot optimize that situation. Same thing when you do something like using a temporary CString object just to format a number in decimal format (a'la "%d" ); the compiler cannot optimize away the dynamic memory allocation or the double-pass required by CString to actually build the string. You should be using an appropriately sized stack buffer with itoa(...) or a similar function.
Not to mention that if you are using external libraries or DLLs, you never know what optimizations were in effect when they were built.
IMHO, asking a developer to rely on the compiler to fix their mistakes is naive at best, and neglegent at worst. Developers should have good enough habits in place so that the compiler has less work to do fixing stupid mistakes.
Peace!
-=- James
Tip for inexperienced drivers: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
your reasons were very good and your right.
compiler cannot optimize many things. most important one of them is memory management. but i think writing wise code that eases optimization for compiler is better than writing dirty code with many manual optimizes
Saleh
|
|
|
|
|
Optimise your code now or your product will have a sad demise. Hang till death.
|
|
|
|
|
who answered "Only when it's critical I do so" ??
like the captain goes with the ship? *looooooooooooool*
i just say, good luck with your next crash!
|
|
|
|
|
you probably mixed up 'debug your code' and 'optimize your code'? just an educated guess.
|
|
|
|
|
i would agree with that. my experience of other people's optimizations is that they most often cause more trouble that they are worth and are equally often very poorly documented.
.dan.g.
AbstractSpoon Software
|
|
|
|
|
hello
please i need a seious help here
i just finished a web application and i want to put it on a web server
but when i did that i faced with a serious problem which is that
my asp.net application didn't work because the web server have not
the .net framework,
so i need any way to modify my web application to work properly on
that web server .
please help me and send me email at
eng_mahmoud_tahoon@hotmail.com
Mahmoud Tahoon
|
|
|
|
|
First, absolutely wrong forum. Try the ASP.NET forum.
Mahmoud Tahoon wrote:
my asp.net application didn't work because the web server have not
the .net framework,
ASP.NET applications required the .NET framework on the server.
Mahmoud Tahoon wrote:
so i need any way to modify my web application to work properly on
that web server .
Other than rewriting all your app in classic ASP, there's no way to do it. Either you get the server to install the .NET framework, or move to another server.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
this is a joke. Each joke contains a little bit of truth
|
|
|
|
|
netpaul wrote:
Moore law is the greatest optimizer
this is a joke. Each joke contains a little bit of truth
Yep...! That was the argument I kept hearing about Java:
"Hey, don't worry! -We'll just put 5 more of those brand new 1GB dual Xeon blades into the server; that'll speed it up... Alright?"
Using Moore's Law as a defense can only work under under one condition; that the new system is running the same OLD CODE as the older one, not newer, even more bloated (or badly written) code. For example, a 3+ year old PIII 500 can run the original Doom or Duke Nuken 3D pretty damn fast. Doom III, on the other hand, is another story entirely!
Peace!
-=- James
Tip for inexperienced drivers: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
Thats what I set in VC6 when I want to improve the speed and that only if the program has alot to do, like graphics processing. If speed is important, then I try to keep in mind what the assembly code will look like for the given piece of code and code accordingly.
Modern day compilers usualy do a better job of optimization than I can. I am not even sure that I've needed to write any optimized assembly (to solve a bottle neck), since VC6 came out. Heck, MS was 60 billion in the black last year, they probably have PHDs designing there compiler.
Of cource for firmware and drivers, someone still has to write the low level hardware access code.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
John R. Shaw wrote:
they probably have PHDs designing there compiler.
Indeed. Whenever someone thinks they can optimize code better than the compiler, my reply is "the compiler is not smarter than you; the people who wrote the compiler are smarter than you, let their compiler do its job."
--Mike--
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb
|
|
|
|
|
...on what kind of optimization you're talking about. A lot of optimization can be done without a lot of extra time and effort spent specifically on optimization.
I always try to utilize good coding practices the first time around, keep in mind the performance cost of each thing I do, and design cleanly and efficiently based on that. Then if the situation warrants further effort in the way of optimization (a graphics processing routine, for example), I will go further.
"A people that values its privileges above its principles soon loses both." -- Dwight D. Eisenhower
|
|
|
|
|
which, in my experience, is GUI drawing. Double-buffering my painting is about the only optimization I worry about these days. The people who write VC are waaaaaaaaaaay smarter than me, I let them do their jobs.
I'm still amazed when I talk to people that don't trust VC to do optimizations. A few weeks ago I was helping out a guy (newbie to Win32 but he had a Unix background) with some rich edit formatting code. He had to set a CHARFORMAT member according to a flag in the app, and I suggested something like:
cf.whatever = some_value;
if ( bIsURL )
cf.whatever |= CF_SOMEFLAG; and he flat-out hated the idea of an extra if, he wanted it all on one line, unless he could "be sure the generated code was faster." (And in fact, that isn't even not trusting the optimizer, it's not being realistic about where your code bottlenecks are...)
--Mike--
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb
|
|
|
|
|
Michael Dunn wrote:
he flat-out hated the idea of an extra if, he wanted it all on one line, unless he could "be sure the generated code was faster."
Talk about inability to prioritise! If the code were in a loop that iterated hundreds of times, maybe, but actually your suggestion is already more optimal than the usual alternative:
if ( bIsURL )
cf.whatever = ( some_value | CF_SOMEFLAG );
else
cf.whatever = some_value; Or the one-line equivalent (same compiler output):
cf.whatever = bIsURL ? ( some_value | CF_SOMEFLAG ) : some_value; You're code is much tighter, as if it even matters in this case!
If he's an assembler junky, show him this (in pseudo x86 assembler):
TEST bIsURL, bIsURL
JZ not_url
MOV EAX, some_value
OR EAX, CF_SOMEFLAG ; stall - must wait on EAX
MOV [cf.whatever], EAX ; stall - wait on EAX again
JMP done ; needless jump
not_url:
MOV [cf.whatever], some_value
done:
As compared to your suggested code:
MOV [cf.whatever], some_value ; very fast
TEST bIsURL, bIsURL
JZ done
OR [cf.whatever], CF_SOMEFLAG ; very fast
done:
Even if he's an eliminate-the-branches freak (which I doubt), the alternative on one line is not necessarily a win, and readability is lost with no realistic performance gain:
cf.whatever = ( some_value | (-(int)bIsURL & CF_SOMEFLAG) ); He needs to put the effort where it's needed, lose the attitude, and show some appreciation.
Cheers
|
|
|
|
|
Sure, compilers are smart and processors are fast. But double-buffering is not a way of code-optimization, but user-interface optimization, because it leads to flicker-free drawing.
On the other hand, if you are i.e. dealing with bitmaps on a pixel level, it makes a lot of sense to directy access the bitmap bits instead of the awesome GetPixel / SetPixel GUI routines.
So code-optimization should also be seen as algorithm-optimization to make clear that it is not only the machine instructions but also the way you're dealing with your data.
The bean-counter example will not lead to any noticable difference in performance. With today (desktop-)processors and PCs, some thousands or millons of instructions must be saved to make a difference.
to make a point:
code optimization means to handle your data better, which is something no compiler can do for me. You can always write bad (and slow) code, or optimize it with better, smarter algorithms.
dirk
|
|
|
|
|
I optimize for Quality, readability and security. And them I will work for speed.
Try this @ home. (B&B)
|
|
|
|
|
I would like to spend some time speculating about this topic. In my first days of coding I was told that a very efficient code is almost unreadable because is very close to the machine assember code. I think today a programmer can find a very good compromise between code efficiency and readibility. With very few, if any, need of ASSEMBLER coding, very efficient C/C++ optimizers, the programmer have to compete with the most important optimizing process: find the better way to do something. Many times I read code that is not efficient because of the algoritm used. In this situation reusing variables, optimizing stack size and reducing memory footprint can do little, and indeed shadows the real problem under a curtain of mistery.
Thanx to all.
|
|
|
|
|
I wrote a program in C++, which solves a problem that uses a recoursive
function, this causes an error like Stack Overflow
I'd like to now, how can I Increase Stacks space??????????
|
|
|
|
|
Hambik wrote:
I'd like to now, how can I Increase Stacks space??????????
You need to think outside the box. Ask yourself how to use less stack space instead. Optimize your code.
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
So I have no another chance???
|
|
|
|
|
If it's that frequent, try to convert it into iterative one.
|
|
|
|
|
I can't, otherwise it will be a very hard algorithm, that will cause another problems
|
|
|
|
|
Look how qsort works internally, its recursive but it never creates a stack overflow: qsort uses (in CRT) goto's...
|
|
|
|