Click here to Skip to main content
15,884,388 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: the outsourcing curse strikes again!! Pin
krsmichael27-Apr-12 14:13
krsmichael27-Apr-12 14:13 
GeneralRe: the outsourcing curse strikes again!! Pin
SASS_Shooter1-May-12 5:49
SASS_Shooter1-May-12 5:49 
GeneralRe: the outsourcing curse strikes again!! Pin
SASS_Shooter1-May-12 5:54
SASS_Shooter1-May-12 5:54 
GeneralI understand that we shouldn't duplicate code, but... Pin
Lutosław16-Apr-12 5:45
Lutosław16-Apr-12 5:45 
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 
For the fun of it I tested your version against another version without the function calls. For this I just manually inlined all calls:
C#
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;
    }
}


Just to be sure to optimize performance I even removed temp variables where possible (possible meaning "without having to calculate the same thing twice):
C#
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));
    }
}


I tested your .NET disassembly code against this and the results show nearly no performance difference for my first alternative and only a minor (10-15% slower!!!) difference for the second one (can anybody tell me why this one is slower?!?). As the .NET code is clearly better readable and maintainable I would say they did everything correct.
Here my test code:
C#
static void Main(string[] args)
        {
            var sw1 = new Stopwatch();
            var sw2 = new Stopwatch();
            var sw3 = new Stopwatch();
            var res = 0;

            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine("{0}. run", j + 1);

                sw1.Reset();
                res = 0;
                sw1.Start();
                for (int i = 0; i < 10000000; i++)
                    res += Tuple1.CombineHashCodes(i, i, i, i, i, i, i, i);
                sw1.Stop();
                Console.WriteLine("Test 1: {0}  Time: {1}", res, sw1.Elapsed);

                sw2.Reset();
                res = 0;
                sw2.Start();
                for (int i = 0; i < 10000000; i++)
                    res += Tuple2.CombineHashCodes(i, i, i, i, i, i, i, i);
                sw2.Stop();
                Console.WriteLine("Test 2: {0}  Time: {1} (+{2:f2}%)", res, sw2.Elapsed, (sw2.ElapsedTicks - sw1.ElapsedTicks) * 100 / sw1.ElapsedTicks);

                sw3.Reset();
                res = 0;
                sw3.Start();
                for (int i = 0; i < 10000000; i++)
                    res += Tuple3.CombineHashCodes(i, i, i, i, i, i, i, i);
                sw3.Stop();
                Console.WriteLine("Test 3: {0}  Time: {1} (+{2:f2}%)", res, sw3.Elapsed, (sw3.ElapsedTicks - sw1.ElapsedTicks) * 100 / sw1.ElapsedTicks);

                Console.WriteLine();
            }

            Console.ReadLine();
        }


The results:
ASM
1. run
Test 1: -1607204864  Time: 00:00:00.0284306
Test 2: -1607204864  Time: 00:00:00.0286526 (+0,78%)
Test 3: -1607204864  Time: 00:00:00.0316660 (+11,38%)

2. run
Test 1: -1607204864  Time: 00:00:00.0281104
Test 2: -1607204864  Time: 00:00:00.0281490 (+0,14%)
Test 3: -1607204864  Time: 00:00:00.0319674 (+13,72%)

3. run
Test 1: -1607204864  Time: 00:00:00.0281114
Test 2: -1607204864  Time: 00:00:00.0285663 (+1,62%)
Test 3: -1607204864  Time: 00:00:00.0313268 (+11,44%)


Just to be complete: The test results where measured in release mode outside of Visual Studio started as a console application. Inside the debugger the original version shows a very significant slowdown.
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 
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. Pin
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 

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.