Performence in c#
-
hi there, I'm new to C# , I have a question abourt performence of c# application. Could you show me some ideal to improve performence ? Thank you, Sincerely,
-
hi there, I'm new to C# , I have a question abourt performence of c# application. Could you show me some ideal to improve performence ? Thank you, Sincerely,
Think out the fastest algorithm. Then implement it the fastest way. C# is very slow, and the compiler can optimize better than you so you can't do anything. Learn asm and you'll get the best performance, both of your mind and of your computer. The one and only Niklas Ulvinge aka IDK
-
Think out the fastest algorithm. Then implement it the fastest way. C# is very slow, and the compiler can optimize better than you so you can't do anything. Learn asm and you'll get the best performance, both of your mind and of your computer. The one and only Niklas Ulvinge aka IDK
-
Try to make a loop that prints all the decimals in pi, or something else. Then make the same in qbasic (an interpreter!) and you'll see that even a twenty year old interpreter is faster than .Net. The one and only Niklas Ulvinge aka IDK
-
Try to make a loop that prints all the decimals in pi, or something else. Then make the same in qbasic (an interpreter!) and you'll see that even a twenty year old interpreter is faster than .Net. The one and only Niklas Ulvinge aka IDK
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".
-
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...