|
The latter is going to be a bit more efficient since the GetRate() function is only called once. Also, since the value of x does not change, the compiler is likely to optimize that by using a register.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I already understand that (not that I usually do ) Just in case you didn't know, I am the programmer formerly known as 'bdiamond'. Anyway, I was talking about for other operations that I might not be familiar with. I wrote in another forum once (please CPians forgive me) that using the '+=' operator was faster with CStrings because I read that in a game programming book. One of the forums moderators told me there was no difference. So I put a breakpoint in my code (release version) in an arbitrary function that I used just to test this by going to debug->windows->disassembly and went back throught the disassembly, and this is what I got for two CStrings that started off as "test" and then had "add" concatenated to them.
str += "add";004015BC push offset string "add" (4037D0h) <br />
004015C1 lea ecx,[esp+8] <br />
004015C5 call dword ptr [__imp_ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::operator+= (4031DCh)] <br />
<br />
str2 = str2 + "add";<br />
004015CB push offset string "add" (4037D0h) <br />
004015D0 lea eax,[esp+4] <br />
004015D4 push eax <br />
004015D5 lea ecx,[esp+10h] <br />
004015D9 push ecx <br />
004015DA call ATL::operator+ (4011C0h) <br />
004015DF add esp,0Ch <br />
004015E2 push eax <br />
004015E3 lea ecx,[esp+4] <br />
004015E7 mov byte ptr [esp+18h],2 <br />
004015EC call dword ptr [__imp_ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::operator= (4031D8h)] <br />
004015F2 lea ecx,[esp+8] <br />
004015F6 call dword ptr [__imp_ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > >::~CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > (403188h)]
int i = 2, x = 2;
int j = x + y;
sprintf("%d",j);
4.7388937 ???
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
One of the underlying reasons why the += operator might be a bit faster is that it (possibly) does not have to copy the source strings (the left side of the += ) for most cases. In VC++ 6.0 Release mode, all CString buffers are allocated in predefined amounts (e.g., 64, 128, 256, and 512 bytes). So, unless the strings are very long, the creation of the concatenated string is an optimized version of a strcat() operation (since it knows the location of the end of the string it doesn't have to search for it, as strcat() would; it just calls memcpy() to the correct place) plus a recomputation of the length of the string. So it is about as efficient as the clumsier pure-C code.
Read here for more.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I'm just wondering, how much of a performance increase could I gain from using inline assembly? I know it's all relative to what I'm doing and so on and so forth. But if every nanosecond counts in a processing loop that dealt with a lot of numbers, would inline assembly make it any faster?
int i = 2, x = 2;
int j = x + y;
sprintf("%d",j);
4.7388937 ???
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
I think, this depends how smart is your compiler. If you will do it in the inline assembly (or in the assembly at all) you know exactly what are you doing so you can take the appropriate optimalization (e.g. using registers and memory as needed), compiler can just guess it from your code in the higher level language. Also, compiler writers can optimize only on some common scenarios.
So in theory, with a supersmart compiler, your assembly and result of the compilation should be equal. But in reality, it always depends how smart your compiler is in that specific situation, and how your high-level language is structured and interpreted by the compiler.
|
|
|
|
|
|
Optimized assembly code can make it faster, but now days the ability of modern complires to optimize the code makes it difficult to improve upon.
Writing your C/C++ code with optimization in mind, is the simplest aproach. Because it makes it easeir for the compiler to optimize the code for you.
-------------------------------------------------
I'll answer you 2nd question here as well.
-------------------------------------------------
You can look at the disassembled code at run-time: View->Debug Windows->Disassembly
If your program is compiled for debugging, that will bring up a mixed assembly and C/C++ source code view. You should place a break point at the point in your code where you want to look at the assembly code. This is mainly used for debugging, in the case where you think the compiler may have messed up (I have not seen this happen in years).
You can also look at the dissembled code for a program compiled for realese by running it as if it where a debug version from VC6, but I am not sure if you can see the whole program.
-------------------------------------------------
-------------------------------------------------
Although with modern compileres (with multiple optimization options) it is not as important, but having some idea of what assembly code your compiler produces for a given piece of C/C++ code, helps you make better decisions on how to write that code (with optimization in mind).
FYI: Airliners have 3 redundant computer systems. Each one is running the same program and each program was compiled using a different compiler. Why? Because each compiler produces code in its own way. That is if one compiler introduce a flaw in the final program, then that same problem will not be on either of the other systems (in theory).
-------------------------------------------------
Learning game programing
-------------------------------------------------
1) Tons of sights and new groups.
2) Graphics Programming By Michael Abrash.
3) http://sourceforge.net/[^];)
:-DGood luck and have fun.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
thanks a bunch. Looks like you put forth some effort and time in posting your answer, and it's appreciated.
int i = 2, x = 2;
int j = x + y;
sprintf("%d",j);
4.7388937 ???
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
Maybe. What processor? In many cases something that will make one processor faster will make a different one slower.
In many cases your compiler can optimise things better than you can (unless you have years to write the whole thing in assembly using the highest priced experts, and even then a new processor is like to come out that needs different optimizations) because it will optimise a larger part of the code.
Never even consider inline assembly until you are convinced (with several eyes looking) that you have the best algorithm for the job, and you know something the compiler doesn't know.
When I was working with RC5 we could get major improvements from 2 lines of assembly, but that was a special case where the compiler did not know about an instruction (ROTL) that is almost never useful, but is critical to that algorithm. In the real world it is rare for such a situation come up.
In most cases the most optimized code can only save you nano-seconds over the less optimized code, and your loop won't be long enough for those nano-second to add up. However there are exceptions. Don't forget that when you go to a different processor (AMD athlon and P4, not just x86 to powerPc) your best optimization changes.
|
|
|
|
|
Henry miller wrote:
and your loop won't be long enough for those nano-second to add up
well, I am thinking about doing some game programming eventually, and that's one of the reasons I asked this question in the first place. Thanks for your response
int i = 2, x = 2;
int j = x + y;
sprintf("%d",j);
4.7388937 ???
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
This is something of a religious subject in that people have certain beliefs and will not change them whatever the argument. And those that do convert, tend to froth at the mouth!
However, IMHO, unless you need to do low-level access to hardware, or you have something in an extremely tight loop, assembly code should not ever be seen.
Remember, the vast majority of the life-cycle of a program is in the maintenance phase and assembly is harder to read and understand than a higher level language. And porting is an issue with assembly, even if on the same processor -- different compilers handle assembly escapes differently.
And modern compilers are pretty good. If you think about how the code is liable to be translated (or peek at the generated code), you can optimize the C/C++ code to make very good machine code. Things like using the natural size integer for a processor rather than something that needs conversion, e.g, use an int rather than a short for a number so low level conversion doesn't have to happen. Naturally, such code should be heavily commented.
As an aside, certain kinds of optimizations that programmers often use because they think they are smarter than the compiler are not needed. For instance, doing x <<= 1; in instead of x *= 2; is almost never needed. The compiler can figure this out just fine. And the first is harder to read...
|
|
|
|
|
You should be aware that this is a religious subject; people of one faith will ignore all arguments from heretics. And converts tend to be evangelistic!
IMHO, unless you need to do low-level hardware access or are in an extremely tight loop, assembly code should never be seen. And if it is, it should be heavily commented. Remember, the vast majority of the life-cycle of a piece of software is in the maintenance phase, not development -- assuming it is a successful program! And assembly is harder to read and hard to port. Even when porting from one compiler to another for the same target processor, porting can be awkward as different compilers have different assembly escapes, stack conventions, internal symbol representations, etc.
Further, modern compilers are smart. Even things like x <<=1; instead of the more readable x *= 2; don't buy you anything as the compiler understands what *= 2 means and will find the best way to accomplish this.
If you really are concerned about optimization in a tight loop, then optimize your algorithm (always your best bang for the buck) and adjust variables such that a minimum of assembly-level conversions are needed. For instance, use the machine's natural size integer (usually int ) instead of a short . If the compiler cannot determine the absolute maximum value and the processor doesn't handle short s as easily as int s, then a low-level conversion might be needed. Try peeking at the generated assembly. And heavily comment such tweaks!
|
|
|
|
|
Guys, how can I know an API constant exact value? For example, PURGE_TXABORT. Any documents?
Thanks a lot.
vigorous
|
|
|
|
|
well, in VS.Net, any windows api constant that I hover over, I get the value. Other than that:
1. put a breakpoint on your code, hover over the constant at run-time ||
2. put the constant in the watch window ||
3. put the constant in a sprintf() or TRACE macro to output the string ||
[edit] 4. right-click on the constant and choose "goto definition" [/edit]
int i = 2, x = 2;
int j = x + y;
sprintf("%d",j);
4.7388937 ???
My articles
www.stillwaterexpress.com
BlackDice
|
|
|
|
|
multiple property pages on one sheet.
i need hide or show one page according to different cases without changing the page's index.
how to do that?
includeh10
|
|
|
|
|
Do you want the tab order the same, or must the index number remain the same (and those of the other tabs)? The latter, to the best of my knowledge, is not possible. For the former, it's probably easiest to remove all of the tabs (using RemovePage) and then add them again (using AddPage), with the page in question on a conditional statement:
void CMyDlg::OnBnClickedShowPgTwo(void)
{
UpdateData();
ResetPropPages();
}
void CMyDlg::ResetPropPages(void)
{
// remove pages - this removes all pages regardless of order or which are showing
while(m_wndPropsht.GetPageCount())
m_wndPropSht.RemovePage(m_wndPropSht.GetPage(0));
// add pages again (order important)
m_wndPropSht.AddPage(&m_wndPg1);
if(m_bShowPg2) m_wndPropSht.AddPage(&m_wndPg2);
m_wndPropSht.AddPage(&m_wndPg3);
}
If you've derived the CPropertySheet class you can do it from within that (no need for "m_wndPropSht."). It doesn't matter if you're showing the property sheet at the time or not, though you need to think about which page to show after resetting, particularly if you were showing the removed page.
|
|
|
|
|
does the code work?
as i tested, if all pages are removed from sheet, the sheet dispears - adding pages again doesn't work.
but i fixed my problem by your idea
thx
includeh10
|
|
|
|
|
sorry, yes, when I did it I always kept the first page which I didn't need to remove.
|
|
|
|
|
Hi all,
How can i know which version of service pack in installed for MSVC6.0 on my machine. and where i can get the SP3 for the MSVC60?
this is because,I am geeting similar errors which are mentioned in Q191626.
Thanks in advance,
Abhi Lahare
|
|
|
|
|
Abhi Lahare wrote:
How can i know which version of service pack in installed for MSVC6.0 on my machine.
How about:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\6.0\ServicePacks
Abhi Lahare wrote:
and where i can get the SP3 for the MSVC60?
Have you tried http://msdn.microsoft.com?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
If you are talking about the visual studio service pack, you can open visual basic and select Help>About. It shows the service pack version but if u have only visual c++ installed, I have no idea
Regards
Pradeep
Between the great things we cannot do and the small things we will not do, the danger is that we shall do nothing
|
|
|
|
|
Perhaps you meant to reply to Abhi Lahare.
david_gilmour wrote:
If you are talking about the visual studio service pack, you can open visual basic and select Help>
Why would I want to open VB to know what service pack is installed?
david_gilmour wrote:
...u have only visual c++ installed, I have no idea
Did you know read my reply in its entirety?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Help:
I'm trying to write an application on Visual Studio .NET 2003 that uses the Video for Windows AVIFile methods to read an AVI DV file and grab some frames from it. It's been working fine, except I can't seem to access the whole of a larger file. If I use AVIFileInfo after AVIFileOpen then the length of the file given (in frames) is smaller than the actual length. If I then open the video stream and use AVIStreamLength I get the actual file length. However, if I try to use AVIStreamGetFrame to extract frames it returns NULL for any requests for frames beyond the length given with AVIFileInfo!
Some messing about has shown me that the cut-off point seems to be around the 1GB mark(approx 4m45s for DV). If I encode using a different scheme (say, Cinepak codec) then it displays well beyond the 5m mark (although the file I checked this on was still less than 1GB, so I need to generate a bigger file to find if the problem resurfaces with a different codec - will do this overnight).
I can't find any reference anywhere to a restriction with reading avis with AVIFile, but can't think what else the problem could be. As far as I know both AVIFileInfo and AVIStreamLength extract data without the use of a codec so it's not a problem with the specific codec that I'm using. Having said that, I know that Video for Windows is a bit outdated and I should really be using DirectShow, so maybe it's because the software is too old...
Has anyone else had this problem or have any ideas?
jOP
|
|
|
|
|
Hi All!
I am getting debug assertion error exactly at the fourth time i open Webbrowser Control, consistently, everytime. CAN ANYONE HELP?
Any help on this regards will be appreciated !
I have two components, one is a MFC Application and the other is a Dll. I have a Webbrowser control in the dll, that is accessed by the MFC App. The following code calls the Webbrowsercontrol, which is in the dll, by the foll code:
"CWebDialog* wb;
wb = new CWebDialog();
wb->DoModal(); "
This code calls the Webbrowser control cpp, in the dll, whose Initdialog has the foll:
BOOL CWebDialog::OnInitDialog()
{
USES_CONVERSION;
CoInitialize(NULL);
try
{
hf_WriteLog("ONINITDLGWEB");
COleVariant vaURL(m_strURL);
((CWebBrowser2*)GetDlgItem(IDC_EXPLORER1))->Navigate2(vaURL, NULL, NULL, NULL, NULL);
hf_WriteLog("NAVIGATED");
VariantClear(&vaURL);
CoUninitialize();
CDialog::OnInitDialog();
}
This throws an exception, every fourth time. Is that a problem with the memory allocation of the dll.. or anything, someone could help????????
Regards
angello6
|
|
|
|
|
Hello
Can anyone please help me to write a multiple column combo box program in VC++.
Thanks
Madhavi
|
|
|
|
|