|
Remember that 90% of bottlenecks are in 10% of the code. Using straight forward C/C++ is actually the best common place optimization since it allows the compiler to do the work. Aside from that, you should get a profile and identify where the bottlenecks are. In some cases, like with hardware access, you can do nothing about performance except upgrade the hardware. In places where you can optimize, I've found that parsing code is a common place to look. Code that uses many small allocations is also a big killer (and problematic in other ways.)
My favorite profile is DevPartner (http://www.compuware.com/products/devpartner/visualc.htm[^]) though it gotten rather expensive as of late.
Added: Building on what John Simmons says--do not optimize code, optimize algorithms. (In other words, no matter how much you try to optimize a bad algorithm, it's still a bad algorithm.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
I come to think of one speed optimisation from my own experience:
Avoid all kinds of (xxx)printf at all cost! Of course including CString.Format, and other functions that use some kind of (xxx)printf.
I once reduced the processing time (for 1000000 iterations) in a loop from 12 seconds to just below 4, simply by replacing one single sprintf() with a number of itoa(), strcat() and similar functions.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Ah memories, I had the exact same experience, also a string object was getting created and initialized to a hard coded value, that was expensive
Usually from my experience the bottleneck problems are in the design!
Also you don't want to create / destroy / initialize a lot of things. If you can cache them or create them once and reuse them that is more ideal. Of course you may still need to reset state variables =) but the cost of creating and destroying an object will have been negated!
If you're optimizing someone else existing code, expect to break it and introduce subtle bugs with the above idea. Chances are you are going to fail to account for properly resetting state variables before reusing one or more object / data structure.
|
|
|
|
|
|
!!!
Yours Truly, The One and Only!
|
|
|
|
|
What is DEP and how to write DEP compatible program?
Amar.
|
|
|
|
|
|
I have a form which is inherited from CDialog.It can be resizing.
Now I want to set its minimum size (300, 200). It couldn't be smaller than (300, 200). How to do it with MFC 6.0.
Thanks in advance.
|
|
|
|
|
Add a message map entry for WM_GETMINMAXINFO and override ...
afx_msg void OnGetMinMaxInfo( MINMAXINFO FAR* lpMMI );
lpMMI->ptMinTrackSize.x = 300
lpMMI->ptMinTrackSize.y = 200;
|
|
|
|
|
Off the top of my head, you should be able to do it by handling WM_GETMINMAXINFO / CWnd::OnGetMinMaxInfo.
Iain.
|
|
|
|
|
Hi,
My application has dependency with lib files.
For db access and other related things there is one lib.
I'm trying to use a retry logic when db connection fails.
The retry logic is implemented in the application.
When a CDBException occurs, it is propagated using throw.
In some functions there is AfxThrowDBException. The problem is in some functions the exception pointer is deleted.
For eg: catch(CDBException* e) {e->Delete();}
So when the throw is propagated, and in the next(need not be immediate outer) catch block if again e is used, i'm getting unhandled exception.
Should I check all the CDBException and remove e? Or is there any other alternative for this?
Thanks and Regards,
sanju.
|
|
|
|
|
why throwing a pointer rather than a reference ?
|
|
|
|
|
What is the advantage of using a reference?
|
|
|
|
|
no need for delete dude !!
|
|
|
|
|
Are you talking about rethrowing a reference to the CDBException pointer or throwing an object
reference?
If throwing an object reference to an object that isn't static - can the object go out of scope
before the exception is caught?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
check what i'm doing in my VisualCalc Parser[^]... full of exceptions references. and it works perfectly well...
|
|
|
|
|
How are you catching the exception in more than one place? If you're rethrowing it then you
shouldn't delete it before doing so. Only the final handler should delete the object.
Unless I'm missing something...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
"Go that way, really fast. If something gets in your way, turn."
|
|
|
|
|
My exact thoughts .. for all piratical purposes the exception has been handed inside the catch block and the propagation stops there!
What remains is how the code execute after this point!
Also as someone said, it's better to throw the object on the stack rather than on the heap and have to deleted it after!
|
|
|
|
|
I had to reread the docs about stack-based exception objects to refresh my mind on the scope of
those objects when they're thrown (see my question to toxcct).
A copy is made if it's caught by value - that's what I forgot
_NightOwl_ wrote: What remains is how the code execute after this point!
"If a matching catch handler is found, and it catches by value, its formal parameter is
initialized by copying the exception object. If it catches by reference, the parameter is
initialized to refer to the exception object. After the formal parameter is initialized, the
process of unwinding the stack begins. This involves the destruction of all automatic objects
that were constructed (but not yet destructed) between the beginning of the try block associated
with the catch handler and the exception's throw site. Destruction occurs in reverse order of
construction. The catch handler is executed and the program resumes execution following the last
handler (that is, the first statement or construct which is not a catch handler). "
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the letter Z
|
|
|
|
|
Mark, yes a copy is always made for all automatic types when passing it around, in fact a copy is made when the object is thrown and placed onto the stack
you just have to vision the exception floating up and backwards through the call chain! Once it's caught by a handler, execution will continue as you found out at the very next statement outside the scope of the catch block. Using a reference means one extra step is spared of not having to copy the exception off the stack and into the catch handler parameter.That slipped my mind till your last note reminded me! and I see your earlier confusion about an object going out of scope before use
|
|
|
|
|
Great description!
Thank you!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
This episode brought to you by the letter Z
|
|
|
|
|
Hi everybody!
I've got a big problem. It would be nice if someone could help me. Thanking you in advance.
I have created 4 tab windows as normal dialog boxes, then I can link those dialogs to the Tab control (using the code given at this website:http://www.codeproject.com/tabctrl/SimpleTab.asp.
In each dialog box, I've got some child controls like : Edit box, check box, button, combo box...
Now, I really want to read data, or to get data , even to exchange data between the different dialogs. How can I make it?
Thanks for your answer anyway.
stephane (Mechanical Ingineer, get starting with VC++, MFC)
|
|
|
|
|
p1 = new CTabPageOne();
p1->Create(IDD_DIALOG_PAGE1,m_ctrlTAB.GetWindow(IDD_DIALOG_PAGE1));
p2 = new CTabPageTwo();
p2->Create(IDD_DIALOG_PAGE2,m_ctrlTAB.GetWindow(IDD_DIALOG_PAGE2));
You have in your main dialog two pointers to your 2 tabs.
So you have a lot of possibilities to exchange data.
For example if you enter a text in a Textbox in Tab1, it should be also entered in Tab2 :
The Main Dialog has the pointers to the Tabs, and each Tab has a pointer to his parent (the Main Dialog)
Now you can make it like this :
CString str;
this->textbox1.GetWindowText(str);
Home->p2->SetValueIntoBox1(str);
where you have a function at tab 2 called CTabPageTwo::SetValueIntoBox1(CString val) which stores
the text into the textbox 1
You even can make a Data-structure which carries all the date to exchange ...
Good luck
Regards
|
|
|
|
|
Hi baerten!
Thanking you for your answer. I try right now.
Best Regards
stephane
|
|
|
|
|
Hello Guys,
i am Vishwanath Patil. i was previously worked on networking technologies SNMP. but now i am working on VC++ and MFC. so i am completely new to these technologies, so i want to learn about these technologies. so if any one who has some good books or sites where it will help for beginners then please replay me.
thanks,
Vishu,
Software Developer,
Target Corporation,
Bangalore.
|
|
|
|