Click here to Skip to main content
15,889,704 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: False selection... Pin
lordofawesome11-Oct-10 5:40
lordofawesome11-Oct-10 5:40 
GeneralRe: False selection... Pin
ely_bob12-Oct-10 3:43
professionalely_bob12-Oct-10 3:43 
GeneralRe: False selection... Pin
ely_bob12-Oct-10 3:39
professionalely_bob12-Oct-10 3:39 
GeneralRe: False selection... Pin
lordofawesome12-Oct-10 4:23
lordofawesome12-Oct-10 4:23 
GeneralRe: False selection... Pin
ely_bob12-Oct-10 5:13
professionalely_bob12-Oct-10 5:13 
GeneralRe: False selection... Pin
James H12-Oct-10 4:02
James H12-Oct-10 4:02 
GeneralRe: False selection... Pin
LockH12-Oct-10 4:13
LockH12-Oct-10 4:13 
GeneralRe: False selection... Pin
waldemar.sauer@aitmetis.com12-Oct-10 7:51
waldemar.sauer@aitmetis.com12-Oct-10 7:51 
This format definitely has an advantage in that if you suspect displayModesMatch of doing something wrong, or otherwise just want to know what it is doing, you only need to set one breakpoint.

I guess both approaches have their merits.

But before we just go ahead and assume that this way is better than that way, let's see how C++ would have done this task. I found the results quite surprising.

To simplify, I used the following code snippet:
bool test1(int dim1, int dim2, int depth1, int depth2, int refresh1, int refresh2)
{
    bool isEqDim = (dim1 == dim2);
    bool isEqDepth = (depth1 == depth2);
    bool isEqRefresh = (refresh1 == refresh2);
    return isEqDim && isEqDepth && isEqRefresh;
}

bool test2(int dim1, int dim2, int depth1, int depth2, int refresh1, int refresh2)
{
    if (dim1 != dim2)
        return false;
    if (depth1 != depth2)
        return false;
    if (refresh1 != refresh2)
        return false;
    return true;
}

void trialrig()
{
    int dim1, dim2, depth1, depth2, refresh1, refresh2;
    scanf("%i", &dim1);
    scanf("%i", &dim2);
    scanf("%i", &depth1);
    scanf("%i", &depth2);
    scanf("%i", &refresh1);
    scanf("%i", &refresh2);
    bool b1_1 = test1(dim1, dim2, depth1, depth2, refresh1, refresh2);
    bool b2_1 = test2(dim1, dim2, depth1, depth2, refresh1, refresh2);
    if (b1_1)
        printf("b1 is true\n");
    if (b2_1)
        printf("b2 is true\n");
}


The scanf's were important, because if they're not there, my compiler just optimized both the test functions out of existence.

During compiling, some of these were inlined, so I have to compensate somewhat. So for simplicity, assume that both code blocks below are prefixed with these instructions:
mov    eax, DWORD PTR _dim1$[esp+88]
mov    ecx, DWORD PTR _dim2$[esp+88]
mov    esi, DWORD PTR _depth1$[esp+88]
mov    edi, DWORD PTR _depth2$[esp+88]
mov    ebx, DWORD PTR _refresh1$[esp+88]
mov    ebp, DWORD PTR _refresh2$[esp+88]


In release build, it produced the following for one of the two functions (excluding the preamble above):
    cmp    eax, ecx
    jne    SHORT $LN7@test12
    cmp    esi, edi
    jne    SHORT $LN7@test12
    cmp    ebx, ebp
    jne    SHORT $LN7@test12
    mov    dl, 1
    jmp    SHORT $LN8@test12
$LN7@test12:
    xor    dl, dl
$LN8@test12:

And the following code for the other function:
    cmp    eax, ecx
    je    SHORT $LN11@test12
    xor    bl, bl
    jmp    SHORT $LN9@test12
$LN11@test12:
    cmp    esi, edi
    je    SHORT $LN10@test12
    xor    bl, bl
    jmp    SHORT $LN9@test12
$LN10@test12:
    cmp    ebx, ebp
    sete    dl
$LN9@test12:


Now just to drive the point home, I'm not going to tell you which of those two code blocks were produced by which function, because as you can see, they're quite similar.

Now, for the first snippet, starting at the cmp, the longest path of execution is 8 instructions, and the shortest path of execution is 3 instructions. For the second snippet, the longest path of execution is 6 instructions, and the shortest path is 4 instructions.

So to summarize, one approach takes 3-8 instructions, whereas the other takes 4-6 instructions.

So if this code was in a real tight loop somewhere, which approach is actually faster depends really on what you expect the outcome to be.

Never assume that you know how the compiler/bytecode interpreter is going to optimize things, and never optimize things without profiling your code to see where the real bottle necks lie.
GeneralRe: False selection... Pin
lordofawesome12-Oct-10 8:26
lordofawesome12-Oct-10 8:26 
GeneralRe: False selection... Pin
waldemar.sauer@aitmetis.com12-Oct-10 8:44
waldemar.sauer@aitmetis.com12-Oct-10 8:44 
GeneralRe: False selection... Pin
lordofawesome12-Oct-10 8:49
lordofawesome12-Oct-10 8:49 
GeneralRe: False selection... Pin
waldemar.sauer@aitmetis.com12-Oct-10 9:06
waldemar.sauer@aitmetis.com12-Oct-10 9:06 
GeneralRe: False selection... Pin
lordofawesome12-Oct-10 9:16
lordofawesome12-Oct-10 9:16 
GeneralRe: False selection... Pin
calamus7712-Oct-10 17:55
calamus7712-Oct-10 17:55 
GeneralRe: False selection... Pin
lordofawesome12-Oct-10 23:24
lordofawesome12-Oct-10 23:24 
GeneralRe: False selection... Pin
calamus7714-Oct-10 15:14
calamus7714-Oct-10 15:14 
GeneralRe: False selection... Pin
werD13-Oct-10 3:54
werD13-Oct-10 3:54 
GeneralRe: False selection... Pin
lordofawesome13-Oct-10 6:42
lordofawesome13-Oct-10 6:42 
GeneralRe: False selection... Pin
Member 451724015-Oct-10 2:39
Member 451724015-Oct-10 2:39 
GeneralRe: False selection... Pin
lordofawesome15-Oct-10 4:17
lordofawesome15-Oct-10 4:17 
GeneralRe: False selection... Pin
supercat911-Oct-10 8:30
supercat911-Oct-10 8:30 
GeneralRe: False selection... Pin
Phil J Pearson11-Oct-10 1:50
Phil J Pearson11-Oct-10 1:50 
GeneralRe: False selection... Pin
David Skelly11-Oct-10 1:53
David Skelly11-Oct-10 1:53 
GeneralRe: False selection... Pin
lordofawesome11-Oct-10 4:28
lordofawesome11-Oct-10 4:28 
GeneralRe: False selection... Pin
PIEBALDconsult11-Oct-10 15:44
mvePIEBALDconsult11-Oct-10 15:44 

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.