Click here to Skip to main content
15,350,151 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I Am the 1% Pin
W Balboos, GHB11-Mar-12 3:47
mveW Balboos, GHB11-Mar-12 3:47 
GeneralRe: I Am the 1% Pin
harold aptroot11-Mar-12 3:57
Memberharold aptroot11-Mar-12 3:57 
GeneralIt sucks to be a Canadian skier in 2012... Pin
Maximilien10-Mar-12 6:06
MemberMaximilien10-Mar-12 6:06 
GeneralRe: It sucks to be a Canadian skier in 2012... Pin
NetDave10-Mar-12 7:32
MemberNetDave10-Mar-12 7:32 
GeneralRe: It sucks to be a Canadian skier in 2012... Pin
AspDotNetDev10-Mar-12 12:24
protectorAspDotNetDev10-Mar-12 12:24 
GeneralRe: It sucks to be a Canadian skier in 2012... Pin
Maximilien10-Mar-12 12:42
MemberMaximilien10-Mar-12 12:42 
JokeBreakfast to go Pin
Ravi Bhavnani10-Mar-12 4:45
professionalRavi Bhavnani10-Mar-12 4:45 
GeneralFun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
Super Lloyd10-Mar-12 4:30
MemberSuper Lloyd10-Mar-12 4:30 
[EDIT2]
Oops.. completely misleading results.. test in x86 mode on my win7 machine!
Well the only conclusion C# is good ^_^, .NET4.5 is not optimized, C++ optimizer is way better
(And, BTW, .NET DLL loading is a sad source of slow .NET app startup)
[/EDIT2]

I was curious about performance and did run some test, on my Windows7 machine.
And also on a Windows 8 (consumer preview) VM machine in Virtual Box on my Windows 7 box.

In C++ I ran this test:
TC++
C++
int primes(unsigned long n) {
unsigned long i, j;
int countprimes = 0;
  for(i = 1; i <= n; i++) {
      int isprime = 1;
      for(j = 2; j < sqrt((double)i); j++) 
          if(!(i%j)) {
        isprime = 0;
        break;
   }
    countprimes+= isprime;
  }
  return countprimes;
}


In C# I have 2 test, one with the SQRT computed before the loop.

In C++ having sqrt called everytime or just before made no difference!!!
I suspect a compiler optimization and tried to trick the compiler with custom no inline function taking both i and j as parameters, no change... :-/

[EDIT]
Thanks Harold I was able to trick the C++ compiler!
with
ASM
static double SQRT(double x)
{
    __asm
    {
        movsd xmm0, x
        sqrtsd xmm0, xmm0
        movsd x, xmm0
    }
    return x;
}

[/EDIT]

TC#1 (Sqrt in the loop)
C#
static int primes(int n)
{
    uint i, j;
    int countprimes = 0;
    for (i = 1; i <= n; i++)
    {
        bool isprime = true;
        var limit = Math.Sqrt(i);
        for (j = 2; j <= Math.Sqrt(i); j++)
            if ((i % j) == 0)
            {
                isprime = false;
                break;
            }
        if (isprime) countprimes++;
    }
    return countprimes;
}


TC#2 (Sqrt before the loop)
C#
static int primes(int n)
{
    uint i, j;
    int countprimes = 0;
    for (i = 1; i <= n; i++)
    {
        bool isprime = true;
        var limit = Math.Sqrt(i);
        for (j = 2; j <= limit; j++)

            if ((i % j) == 0)
            {
                isprime = false;
                break;
            }
        if (isprime) countprimes++;
    }
    return countprimes;
}


Now for the times... (took the average time of a few run, with n = 1000 000)
Windows 7:
TC#1: 2780 ms (.NET4)
TC#2: 858 ms (.NET4)
TC#1: 2400 ms (.NET3.5)
TC#2: 856 ms (.NET3.5)
TC++: 286 ms
[EDIT]TC++ (inline sqrt): 1011 ms[/EDIT]


Windows 8:
TC#1: 1065 ms (.NET4)
TC#2: 296 ms (.NET4)
TC#1: 2760 ms (.NET4.5)
TC#2: 880 ms (.NET4.5)
TC++: 297 ms

For this results it appears that
1. Math.Sqrt() is slooooow Frown | :(
or the C++ compiler defied my attempt at preventing it optimizing

2. (.NET on) Windows 8 is fast!!! .NET4 on my VM is faster than .NET4 on the host machine!!!
OMG | :OMG:

3. .NET4.5 is not yet optimized...

4. WTF happened to Math.Sqrt() in .NET4??! it is slower than .NET3.5!!!?!?! WTF | :WTF: Mad | :mad:


[EDIT]
5. C# is not doing too bad after all! Smile | :)
From some Windows Phone profiling I guess the biggest perceived performance (real) problem is DLL loading, C# is slow.. (at startup)...
[/EDIT]
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.


modified 10-Mar-12 11:10am.

GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
harold aptroot10-Mar-12 4:34
Memberharold aptroot10-Mar-12 4:34 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
Super Lloyd10-Mar-12 4:54
MemberSuper Lloyd10-Mar-12 4:54 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
harold aptroot10-Mar-12 5:02
Memberharold aptroot10-Mar-12 5:02 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
Super Lloyd10-Mar-12 5:09
MemberSuper Lloyd10-Mar-12 5:09 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
Super Lloyd10-Mar-12 5:00
MemberSuper Lloyd10-Mar-12 5:00 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
#realJSOP10-Mar-12 5:52
mva#realJSOP10-Mar-12 5:52 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
Super Lloyd10-Mar-12 13:16
MemberSuper Lloyd10-Mar-12 13:16 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
NormDroid10-Mar-12 6:35
professionalNormDroid10-Mar-12 6:35 
GeneralRe: Fun performance test on Win7/Win8 with C++, C#(3.5,4,4.5) and Math.Sqrt() as special guest Pin
harold aptroot10-Mar-12 6:42
Memberharold aptroot10-Mar-12 6:42 
GeneralBackwards flying space shuttle... Pin
Sander Rossel10-Mar-12 0:27
professionalSander Rossel10-Mar-12 0:27 
GeneralRe: Backwards flying space shuttle... Pin
Nelek10-Mar-12 1:13
protectorNelek10-Mar-12 1:13 
GeneralIt's official: the end of the world is tomorrow. Pin
Septimus Hedgehog9-Mar-12 22:39
MemberSeptimus Hedgehog9-Mar-12 22:39 
GeneralRe: It's official: the end of the world is tomorrow. Pin
Richard MacCutchan9-Mar-12 22:40
mveRichard MacCutchan9-Mar-12 22:40 
GeneralRe: It's official: the end of the world is tomorrow. Pin
User 592419-Mar-12 23:00
MemberUser 592419-Mar-12 23:00 
GeneralRe: It's official: the end of the world is tomorrow. Pin
Dalek Dave9-Mar-12 23:11
professionalDalek Dave9-Mar-12 23:11 
GeneralRe: It's official: the end of the world is tomorrow. Pin
chriselst9-Mar-12 23:26
Memberchriselst9-Mar-12 23:26 
GeneralRe: It's official: the end of the world is tomorrow. Pin
Super Lloyd9-Mar-12 23:53
MemberSuper Lloyd9-Mar-12 23:53 

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.


Straw Poll

What language are you using for AI development or learning?
Whether you're at work or home, hard core coding or just trying to figure out this AI thing, which languages have you used in your adventures?
  Results   293 votes