Click here to Skip to main content
15,889,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Calibration in ATM machines Pin
GKarRacer5-Aug-05 10:11
GKarRacer5-Aug-05 10:11 
GeneralRe: Calibration in ATM machines Pin
celllllllll5-Aug-05 11:53
celllllllll5-Aug-05 11:53 
Generalproblem in CFtpConnection::OpenFile() Pin
Aditya Rao3-Aug-05 7:47
Aditya Rao3-Aug-05 7:47 
GeneralRe: problem in CFtpConnection::OpenFile() Pin
Anonymous3-Aug-05 12:13
Anonymous3-Aug-05 12:13 
Generalremoving spaces in string Pin
Anonymous3-Aug-05 7:39
Anonymous3-Aug-05 7:39 
GeneralRe: removing spaces in string Pin
David Crow3-Aug-05 7:52
David Crow3-Aug-05 7:52 
GeneralRe: removing spaces in string Pin
Anonymous3-Aug-05 12:15
Anonymous3-Aug-05 12:15 
GeneralRe: removing spaces in string Pin
Jose Lamas Rios3-Aug-05 19:06
Jose Lamas Rios3-Aug-05 19:06 
Anonymous wrote:
str[y++] = str[x];
is a bit more efficient


Are you sure? It may be in theory, but in practice, the compiler will most likely optimize both versions of the source code into exactly the same object code.

I made a simple test with the following code:
#pragma auto_inline(off)
 
void Test(int x, int y, char* str)
{
   str[y] = str[x];
   y++;
}
 
void Test2(int x, int y, char* str)
{
   str[y++] = str[x];
}

I wrote each of these functions in separate source files, and called each of them from a function in a third file.

In a Release build, with the default optimizations, the compiler not only detected they were the same thing, but in fact removed Test2 entirely, and changed the call to it, making it call Test instead.

This was the dissambly for Test()
void Test(int x, int y, char* str)
{
   str[y] = str[x];
004016C0  mov         eax,dword ptr [esp+0Ch] 
004016C4  mov         ecx,dword ptr [esp+4] 
004016C8  mov         dl,byte ptr [ecx+eax] 
004016CB  mov         ecx,dword ptr [esp+8] 
004016CF  mov         byte ptr [ecx+eax],dl 
   y++;
}
004016D2  ret


[Update]
After submitting this post I realized that I should have make the y parameter a reference:
//void Test(int x, int y, char* str)
void Test(int x, int& y, char* str)
{
   str[y] = str[x];
   y++;
}


In the previous version, it didn't make sense to increment y, because that increment won't be seen outside of the function. Of course, the compiler spotted this and didn't even bothered to generate code for it.

After the modifications, the results were exactly the same: Test2 was not generated, and Test was called instead. The only change was the generated code for Test
void Test(int x, int& y, char* str)
{
   str[y] = str[x];
004016D0  mov         eax,dword ptr [esp+8] 
004016D4  mov         ecx,dword ptr [esp+0Ch] 
004016D8  mov         edx,dword ptr [eax] 
004016DA  push        ebx  
004016DB  push        esi  
004016DC  mov         esi,dword ptr [esp+0Ch] 
004016E0  mov         bl,byte ptr [esi+ecx] 
004016E3  mov         byte ptr [ecx+edx],bl 
   y++;
004016E6  add         dword ptr [eax],1 
004016E9  pop         esi  
004016EA  pop         ebx  
}
004016EB  ret              

[End of Update]

In general, focusing on optimizing that kind of details in the source code is a waste of time, and in some cases it may even penalize code clarity for no real benefit.


--
jlr
http://jlamas.blogspot.com/[^]
GeneralRe: removing spaces in string Pin
David Crow7-Aug-05 8:15
David Crow7-Aug-05 8:15 
General,remove spaces in a string Pin
Anonymous3-Aug-05 7:38
Anonymous3-Aug-05 7:38 
GeneralRe: ,remove spaces in a string Pin
Chris Meech3-Aug-05 8:16
Chris Meech3-Aug-05 8:16 
GeneralSoapToolkit Pin
Ed K3-Aug-05 7:37
Ed K3-Aug-05 7:37 
GeneralRe: SoapToolkit Pin
basementman3-Aug-05 8:06
basementman3-Aug-05 8:06 
GeneralCCombo :( Pin
Smith#3-Aug-05 7:17
Smith#3-Aug-05 7:17 
GeneralRe: CCombo :( Pin
Marc Soleda3-Aug-05 7:30
Marc Soleda3-Aug-05 7:30 
QuestionHow can i send a command to cmd.exe? Pin
Lagwagon563-Aug-05 7:04
Lagwagon563-Aug-05 7:04 
AnswerRe: How can i send a command to cmd.exe? Pin
David Crow3-Aug-05 7:46
David Crow3-Aug-05 7:46 
AnswerRe: How can i send a command to cmd.exe? Pin
Gurra_Koo3-Aug-05 9:48
Gurra_Koo3-Aug-05 9:48 
GeneralRe: How can i send a command to cmd.exe? Pin
Lagwagon563-Aug-05 10:18
Lagwagon563-Aug-05 10:18 
GeneralRe: How can i send a command to cmd.exe? Pin
Gurra_Koo3-Aug-05 10:22
Gurra_Koo3-Aug-05 10:22 
GeneralRe: How can i send a command to cmd.exe? Pin
Lagwagon563-Aug-05 11:48
Lagwagon563-Aug-05 11:48 
GeneralRe: How can i send a command to cmd.exe? Pin
Gurra_Koo3-Aug-05 11:52
Gurra_Koo3-Aug-05 11:52 
GeneralRe: How can i send a command to cmd.exe? Pin
Lagwagon564-Aug-05 10:05
Lagwagon564-Aug-05 10:05 
GeneralNeed Help ....MultiThread Pin
inbakumar.G3-Aug-05 4:58
inbakumar.G3-Aug-05 4:58 
GeneralRe: Need Help ....MultiThread Pin
Blake Miller3-Aug-05 10:33
Blake Miller3-Aug-05 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.