Performence in c#
-
I didn't say .NET is on par or even faster than other languages, but stating that C# is "very slow" is absolutely not true. For example MDX applications are about 6 to 10 percent slower than their C++ counterparts, sometimes even less. That's actually pretty good considering all the .NET "overhead".
Have you ever tried what I descibed? I still think .net is the absolutly slowest platform I've ever programmed on. The one and only Niklas Ulvinge aka IDK
-
Have you ever tried what I descibed? I still think .net is the absolutly slowest platform I've ever programmed on. The one and only Niklas Ulvinge aka IDK
Could you post your code (C# and QBASIC)? Because you must have done something extremly wrong in the C# version to get such a low performance.
-
Could you post your code (C# and QBASIC)? Because you must have done something extremly wrong in the C# version to get such a low performance.
C#:
static public void cpi() { do { pi += 1 / a; pi -= 1 / (a + 2); a += 4; Console.WriteLine(pi * 4); } while (true); }
Qbasic:
DEFDBL A-Z 'DEFLNG B CLS b = 1 DO ab = a a = a + 1 / b a = a - 1 / (b + 2) b = b + 4 LOCATE 1, 1: PRINT a * 4, b LOOP
The one and only Niklas Ulvinge aka IDK
-
C#:
static public void cpi() { do { pi += 1 / a; pi -= 1 / (a + 2); a += 4; Console.WriteLine(pi * 4); } while (true); }
Qbasic:
DEFDBL A-Z 'DEFLNG B CLS b = 1 DO ab = a a = a + 1 / b a = a - 1 / (b + 2) b = b + 4 LOCATE 1, 1: PRINT a * 4, b LOOP
The one and only Niklas Ulvinge aka IDK
The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.
-
C#:
static public void cpi() { do { pi += 1 / a; pi -= 1 / (a + 2); a += 4; Console.WriteLine(pi * 4); } while (true); }
Qbasic:
DEFDBL A-Z 'DEFLNG B CLS b = 1 DO ab = a a = a + 1 / b a = a - 1 / (b + 2) b = b + 4 LOCATE 1, 1: PRINT a * 4, b LOOP
The one and only Niklas Ulvinge aka IDK
-
The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.
-
Dammit mine takes almost 2.7 seconds, what CPU do you have? I thought my 3.0 GHz should be fine :sigh: :-D
It's very slow. That's what it's meant to be. The one and only Niklas Ulvinge aka IDK
-
The flaw in your benchmark is that 99% of the time are used for output of the results and less than 1% is needed for the calculation. You're not comparing C# to QBasic, you're comparing PRINT to Console.WriteLine. Console.WriteLine will look at the Windows settings for your preferred decimal separator character etc. You can't compare it to QBasic's PRINT. Moreover, a lot of time will be used by the console window to render the output. Here it can be a big difference if you reset the cursor position or let the window scroll. Try this: static public void cpi() { int start = Environment.TickCount; double pi = 0; double a = 1; for (int i = 0; i < 100000000; i++) { pi += 1 / a; pi -= 1 / (a + 2); a += 4; } Console.WriteLine(pi * 4); Console.WriteLine("Finished after " + (Environment.TickCount - start) + " ms"); Console.ReadKey(); } - against - DEFDBL A-Y DEFLNG Z CLS Y = TIMER b = 1 FOR Z = 0 TO 1000000 A = A + 1 / b A = A - 1 / (b + 2) b = b + 4 NEXT PRINT A * 4 PRINT TIMER - Y CSharp for 100000000 iterations: 1750 ms QBASIC for 1000000 iterations: 4 s This means C# is about 230 times as fast as QBASIC for raw calculations. One could still use C# and P/Invoke to an assembler-written PRINT function if you really need to output large amounts of doubles to the console.
That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK
-
That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK
I have an AMD 64 (3500+) here. Oh, and I used a release build without integer overflow checks, which again is unfair since QBasic does not support those optimizations. But I think there's no way to create a fair benchmark. You can create a benchmark that shows pretty much anything if you want. Write to the console and QBasic is faster than C#. Use Math.Sin and C# is 50 times faster than Java 1.4. Construct lots of objects on the heap and C# is faster than C (without using memory pooling). So the best language always depends on what you want to do. If you need optimal performance, C is the way to go (maybe even use assembler for some sections). C# is pretty fast, but it's easy to ruin performance by using some slow method. But this is the same as in every language, some functions always are slow. Console.WriteLine is something that is not performance-critical in nearly all C# applications, so it didn't made sense for Microsoft to write a fast implementation. You get much better performance in C# if you write to a file instead of the console. As a side note, there are algorithms that need only 50 iterations to get PI so exactly as a double can store it. What other .NET functions do you think are slow and would be useful inside critical loops? Console.WriteLine is not one of those for me.
-
That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK
Have you only come to this forum to post such crap? Have you tried to understand what Daniel was saying? Console.WriteLine as a very complex beast. You can feed it with any kind of data and it will print it out different ways automatically depending on the set culture. Is QBasic also able to do this with its PRINT function?
-
I have an AMD 64 (3500+) here. Oh, and I used a release build without integer overflow checks, which again is unfair since QBasic does not support those optimizations. But I think there's no way to create a fair benchmark. You can create a benchmark that shows pretty much anything if you want. Write to the console and QBasic is faster than C#. Use Math.Sin and C# is 50 times faster than Java 1.4. Construct lots of objects on the heap and C# is faster than C (without using memory pooling). So the best language always depends on what you want to do. If you need optimal performance, C is the way to go (maybe even use assembler for some sections). C# is pretty fast, but it's easy to ruin performance by using some slow method. But this is the same as in every language, some functions always are slow. Console.WriteLine is something that is not performance-critical in nearly all C# applications, so it didn't made sense for Microsoft to write a fast implementation. You get much better performance in C# if you write to a file instead of the console. As a side note, there are algorithms that need only 50 iterations to get PI so exactly as a double can store it. What other .NET functions do you think are slow and would be useful inside critical loops? Console.WriteLine is not one of those for me.
Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK
-
Have you only come to this forum to post such crap? Have you tried to understand what Daniel was saying? Console.WriteLine as a very complex beast. You can feed it with any kind of data and it will print it out different ways automatically depending on the set culture. Is QBasic also able to do this with its PRINT function?
No, but it's faster. The one and only Niklas Ulvinge aka IDK
-
Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK
I also like to use Console.WriteLine for debugging. WriteLine is slower than PRINT, but both write extremly faster than you can read the output. If your output is useful for debugging, it probably isn't so long that it can have any impact on the program.
-
I also like to use Console.WriteLine for debugging. WriteLine is slower than PRINT, but both write extremly faster than you can read the output. If your output is useful for debugging, it probably isn't so long that it can have any impact on the program.
One example of the performance loss of the Console.WriteLine func is when debugging the program to calc pi. Since it goes in very fast loop it is very slow. If you think my program was a bad example, I got a NN program that's built in C#. It needs to write variables at runtime every loop. The one and only Niklas Ulvinge aka IDK
-
No, but it's faster. The one and only Niklas Ulvinge aka IDK
I have only one reply : LOL But not a simple LOL, a very loud LOL. I have never seen such a stubborn man. Btw, Niklas I wish you luck in writing an ERP in assembler :) I hope you understand...because is a rough world out there...
-
I have only one reply : LOL But not a simple LOL, a very loud LOL. I have never seen such a stubborn man. Btw, Niklas I wish you luck in writing an ERP in assembler :) I hope you understand...because is a rough world out there...
What would be hard to write an ERP in assembler? The one and only Niklas Ulvinge aka IDK
-
That was the point. C#'s out func and a lot other funcs it got is really slow. The one and only Niklas Ulvinge aka IDK
-
OK, I meant the .NET framework... C# is compiled to msil, wich is the .net framework language... Another way of writing it is: The out func of C# and a lot other funcs it got is really slow Wich meant what I meant... The one and only Niklas Ulvinge aka IDK
-
Writeline is important for me... It's my debugger... Now I write almost everyting in asm, as it's so fast and easy to get. It got enought documentation to make me sick, that's the way I love it. The one and only Niklas Ulvinge aka IDK
-
OK, I meant the .NET framework... C# is compiled to msil, wich is the .net framework language... Another way of writing it is: The out func of C# and a lot other funcs it got is really slow Wich meant what I meant... The one and only Niklas Ulvinge aka IDK
What I'm getting at is that C# isn't slow. The particular method you are using is slow because it takes into account a whole host of things like culture, formatting options and so on. If you only ever want to output something in a very simple way then Console.WriteLine is too feature rich, and that is what you are paying for. There is nothing stopping you from providing your own implementation if you need speed.
My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious