tags:

views:

695

answers:

6
+1  Q: 

How fast is c#?

Is c# compiled to machine code, or is it compiled into a platform independent intermediate code?

I am looking at an app that needs to be ported to a single language, and we would like to port it to a portable/fast platform.

+13  A: 

It's both: it's compiled to intermediate code, deployed, and then compiled to machine code on the machine on which it's installed and/or run.

ChrisW
This answer is misleading. C# is not compiled to machine code. It is compiled to intermediate code and it is this that is compiled to machine code at runtime by the JIT compiler. The JIT compiler does know about C#, VB.NET, F# etc, only that it is dealing with IL.
Ash
Yes, the intermediate code isn't C# anymore; although, the intermediate language *can* be decompiled back to C#. I thought it wasn't misleading: just a simple, not-too-pedantic answer to the OP.
ChrisW
@ChirsW, yes a bit pedantic, but the whole thing about .NET and the CLR is that once you've compiled your code, the language it was written in is irrelevant. (CLR via C# is a great book that clarifies all of this).
Ash
The OP only wanted to know whether code that's written in C# is portable and/or fast.
ChrisW
@ChrisW, C# is not "deployed" anywhere, and is not "compiled to machine code on the machine on which it's installed/run". Could be seen as pedantic, but needs to be said as too many devs still don't understand this.
Ash
+2  A: 

C# is fast. It's compiled into a byte code that is then translated by a JIT. There's an open source implementation of C# called Mono that works great. I have basic ASP.NET sites up and written in C# on Linux and Mono and they run great.

thaBadDawg
theBadDawg, any good websites that detail all missing features if you were going to use ASP.NET on top of Mono? I'm going to be working on some sidework that if needed later I would like to scale up using Linux/Mono/MySQL
tyndall
+3  A: 

Check out http://shootout.alioth.debian.org/gp4/csharp.php for some benchmark comparisons.

Gentoo Pentium 4 aren't the freshest measurements on the benchmarks game website!
igouy
Also, why do the test on Linux with Mono? Java runs fine under Windows. Makes you wonder if they are afraid to run head-up against the real .NET framework.
David Leon
+1  A: 

It doesn't sound like you care how fast C# is, you just want it to be not noticeably slow. For that, the answer is most certainly yes. There are many impressive applications written in C#. Unless you are doing something extremely processor intensive, it shouldn't seem any slower than C++.

Steve Rowe
There is process intensive code that needs to be run.
Milhous
A: 

Is c# compiled to machine code?

No.

Is it compiled into a platform independent intermediate code?

Yes.

OscarRyz
That answer might give someone the mistaken impression that, at run-time, C# is interpreted rather than compiled to machine code.
ChrisW
Platform independent so long as that platform is a variant of Windows. :)
cletus
@cletus: or a variant of Linux, OSX, see Mono.
Ash
@Chris. It is actually interpreted at runtime, it turns out the interpreter is a very good one and uses machine code in its interpretation. If you restart the machine, does the machine code generated runs again or does the interpreter re-compiles it. ( I don't know the answer my self to be honest )
OscarRyz
@Chris: At least that's what the JVM does :) The interpreter generate assembly when possible and inline as much as it is capable, but once the interpreter dies, all that get lost.
OscarRyz
@Oscar No, it's compiled Just In Time before it's run; e.g. a loop is compiled once (before it's run) and is then pure machine code (so, it runs faster than any interpreter). It can either be recompiled each time before it's run (so, there's some startup delay), or compiled once when it's installed.
ChrisW
@Chris. So, the next day when you start your machine and run your .net app, what gets run pure machine code, because it was compiled by the interpreter the day before. Is that what you're saying?
OscarRyz
No it's either compiled when it's first installed, or it's recompiled each time it's loaded just before before it's run; but it is compiled before it's run: there's a JIT compiler, not a run-time interpreter ... the code that's being run is fast, compiled machine code.
ChrisW
@Chris, @Oscar, I'm no expert on this but I recommend reading CLR via C#, by Jeff Richter. It clearly describes the double compilation process of .NET languages. From that, C# is compiled to a PE file containing IL. At runtime this IL is compiled by the JIT to native machine code.
Ash
@Chris: Ahh ok, that's what I thought, we agree then. That's also what the Java interpreter does. Even V8 on Google chrome does that. But in my point of view, that's the interpreter being veeeery good at its job and creates assembly. But the next day it has to re-do it. That's interpretation for me
OscarRyz
@Oscar: You shouldn't redefine terms like that. It's compilation for everyone else. ,-)
Rytmis
@Rytmis. Ok, what about this. C# is compiled to intermediate bytecode, then compiled into machine code ( assembly ) by the interpreter and once you shutdown the program ( or the machine ) only the bytecode remains. Next time the program is run the interpreter will re-compile it to machine code. :)
OscarRyz
It's called "the Common Language Runtime's Just-In-Time compiler" (the CLR's JIT compiler), not "the interpreter".
ChrisW
An important difference between a compiler and an interpreter is that an inpreter is running *while* the application is executing: the CPU runs the interpreter, which reads and executes the IL. Whereas a compiler runs *before* the application.
ChrisW
@ChrisW: Then it will be: C# is compiled to intermediate bytecode and re-compiled to machine code each time it is run by the CLR JIT compiler. That would be better Chris? :)
OscarRyz
Just to be a nerd here, machine code is interpreted. It's interpreted by the microcode.
Mike Dunlavey
+3  A: 

From what I understand, Java tends to be a little faster than C#, but not enough for it to really matter for most applications, while C# tends to require less memory. They tend to be good at similar types of applications when compared to other languages. They usually perform slower than C++, but in some applications they can be faster because of optimizations that the JIT compiler can make. They generally are an order of magnitude faster than languages that are normally purely interpreted, like Perl or Python.

You specifically mentioned C#, but I bring up Java because it is better supported cross-platform, which you also specifically mention. Thanks to Mono, you can do C# developement cross-platform, which now apparently has support for windows forms. However, you may need to be careful if some of the team uses .Net and some of it uses Mono because Mono only supports up to C# 2.0 and Microsoft is currently working on version 4.0 and is fairly aggressive about pushing new features. Java has the exact opposite problem where Sun is very conservative about pushing new features, causing it to lack some of the nice features that C# has, like properties and closures.

James