Click here to Skip to main content
15,891,253 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: I understand that we shouldn't duplicate code, but... Pin
Brisingr Aerowing16-Apr-12 5:54
professionalBrisingr Aerowing16-Apr-12 5:54 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
harold aptroot16-Apr-12 7:23
harold aptroot16-Apr-12 7:23 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Lutosław16-Apr-12 9:31
Lutosław16-Apr-12 9:31 
GeneralRe: I understand that we shouldn't duplicate code, but... PinPopular
Robert Rohde16-Apr-12 13:26
Robert Rohde16-Apr-12 13:26 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Lutosław17-Apr-12 0:46
Lutosław17-Apr-12 0:46 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Robert Rohde17-Apr-12 19:03
Robert Rohde17-Apr-12 19:03 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Lutosław17-Apr-12 23:12
Lutosław17-Apr-12 23:12 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Robert Rohde18-Apr-12 10:45
Robert Rohde18-Apr-12 10:45 
I really don't see why this should make a difference here, but to avoid a big discussion on how the JIT works I rewrote my tests.
First I make one test run just for the JIT. Then I make 1000 test runs for each test case and in each test run 10.000.000 calls to CombineHashCodes are done.

Here the results:
ASM
Test 1: -1607204864  Time Avg: 00:00:00.0281418 (100,00%)
Test 2: -1607204864  Time Avg: 00:00:00.0281436 (100,01%)
Test 3: -1607204864  Time Avg: 00:00:00.0312717 (111,12%)

I feel the JIT optimizer does inline those nested calls exactly as I did.

Here the complete code:
C#
class Program
{
    static void Main(string[] args)
    {
        var sw1 = new Stopwatch();
        var sw2 = new Stopwatch();
        var sw3 = new Stopwatch();
        var res1 = 0;
        var res2 = 0;
        var res3 = 0;

        res1 = Test1(sw1, res1);
        res2 = Test2(sw2, res2);
        res3 = Test3(sw3, res3);
        //first run is for the JIT -> Reset stopwatches
        sw1.Reset();
        sw2.Reset();
        sw3.Reset();

        int noOfRuns = 1000;
        for (int j = 0; j < noOfRuns; j++)
        {
            res1 = Test1(sw1, res1);
            res2 = Test2(sw2, res2);
            res3 = Test3(sw3, res3);
        }

        var avg1 = sw1.Elapsed.Ticks / noOfRuns;
        var avg2 = sw2.Elapsed.Ticks / noOfRuns;
        var avg3 = sw3.Elapsed.Ticks / noOfRuns;

        Console.WriteLine("Test 1: {0}  Time Avg: {1} ({2:f2}%)", res1, new TimeSpan(avg1), 100.0);
        Console.WriteLine("Test 2: {0}  Time Avg: {1} ({2:f2}%)", res2, new TimeSpan(avg2), avg2 * 100.0 / avg1);
        Console.WriteLine("Test 3: {0}  Time Avg: {1} ({2:f2}%)", res3, new TimeSpan(avg3), avg3 * 100.0 / avg1);

        Console.ReadLine();
    }

    private static int Test3(Stopwatch sw3, int res3)
    {
        res3 = 0;
        sw3.Start();
        for (int i = 0; i < 10000000; i++)
            res3 += Tuple3.CombineHashCodes(i, i, i, i, i, i, i, i);
        sw3.Stop();
        return res3;
    }

    private static int Test2(Stopwatch sw2, int res2)
    {
        res2 = 0;
        sw2.Start();
        for (int i = 0; i < 10000000; i++)
            res2 += Tuple2.CombineHashCodes(i, i, i, i, i, i, i, i);
        sw2.Stop();
        return res2;
    }

    private static int Test1(Stopwatch sw1, int res1)
    {
        res1 = 0;
        sw1.Start();
        for (int i = 0; i < 10000000; i++)
            res1 += Tuple1.CombineHashCodes(i, i, i, i, i, i, i, i);
        sw1.Stop();
        return res1;
    }
}

public static class Tuple1
{
    //...
    internal static int CombineHashCodes(int h1, int h2)
    {
        return (h1 << 5) + h1 ^ h2;
    }

    internal static int CombineHashCodes(int h1, int h2, int h3)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2), h3);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2), Tuple1.CombineHashCodes(h3, h4));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2, h3, h4), h5);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2, h3, h4), Tuple1.CombineHashCodes(h5, h6));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2, h3, h4), Tuple1.CombineHashCodes(h5, h6, h7));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
    {
        return Tuple1.CombineHashCodes(Tuple1.CombineHashCodes(h1, h2, h3, h4), Tuple1.CombineHashCodes(h5, h6, h7, h8));
    }
}


public static class Tuple2
{
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
    {
        var h12 = (h1 << 5) + h1 ^ h2;
        var h34 = (h3 << 5) + h3 ^ h4;
        var h1234 = (h12 << 5) + h12 ^ h34;
        var h56 = (h5 << 5) + h5 ^ h6;
        var h78 = (h7 << 5) + h7 ^ h8;
        var h5678 = (h56 << 5) + h56 ^ h78;
        return (h1234 << 5) + h1234 ^ h5678;
    }
}

public static class Tuple3
{
    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
    {
        var h12 = (h1 << 5) + h1 ^ h2;
        var h34 = (h3 << 5) + h3 ^ h4;
        var h1234 = (h12 << 5) + h12 ^ h34;
        var h56 = (h5 << 5) + h5 ^ h6;
        return (h1234 << 5) + h1234 ^ ((h56 << 5) + h56 ^ ((h7 << 5) + h7 ^ h8));
    }
}

GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Johann Gerell17-Apr-12 21:35
Johann Gerell17-Apr-12 21:35 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Bruce Patin18-Apr-12 4:20
Bruce Patin18-Apr-12 4:20 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
ClockMeister18-Apr-12 2:17
professionalClockMeister18-Apr-12 2:17 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
alcexhim18-Apr-12 5:50
alcexhim18-Apr-12 5:50 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
ClockMeister18-Apr-12 6:27
professionalClockMeister18-Apr-12 6:27 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Robert Rohde18-Apr-12 11:04
Robert Rohde18-Apr-12 11:04 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
ClockMeister18-Apr-12 11:27
professionalClockMeister18-Apr-12 11:27 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
Richard Deeming18-Apr-12 9:02
mveRichard Deeming18-Apr-12 9:02 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
ClockMeister18-Apr-12 9:50
professionalClockMeister18-Apr-12 9:50 
GeneralRe: I understand that we shouldn't duplicate code, but... Pin
TRK318-Apr-12 6:50
TRK318-Apr-12 6:50 
GeneralYou know you've been staring at code for too long when you write this test. PinPopular
Pete O'Hanlon16-Apr-12 0:38
mvePete O'Hanlon16-Apr-12 0:38 
GeneralRe: You know you've been staring at code for too long when you write this test. Pin
Brisingr Aerowing16-Apr-12 1:40
professionalBrisingr Aerowing16-Apr-12 1:40 
GeneralRe: You know you've been staring at code for too long when you write this test. Pin
CDP180216-Apr-12 2:02
CDP180216-Apr-12 2:02 
GeneralRe: You know you've been staring at code for too long when you write this test. Pin
Brisingr Aerowing16-Apr-12 4:53
professionalBrisingr Aerowing16-Apr-12 4:53 
GeneralRe: You know you've been staring at code for too long when you write this test. Pin
Pete O'Hanlon16-Apr-12 5:02
mvePete O'Hanlon16-Apr-12 5:02 
GeneralRe: You know you've been staring at code for too long when you write this test. Pin
Brisingr Aerowing16-Apr-12 5:18
professionalBrisingr Aerowing16-Apr-12 5:18 
GeneralSQL Server Management Studio's ordered view Pin
Bernhard Hiller11-Apr-12 4:39
Bernhard Hiller11-Apr-12 4:39 

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.