Home > front end >  Do all compiled codes have same speed no matter what language they were written in?
Do all compiled codes have same speed no matter what language they were written in?

Time:10-23

Suppose I write a program in both Python and C and I turn these to executable. Now, will both the executable have the same speed or will it vary (I guess it shouldn't cause it should now be in machine code form) ?

CodePudding user response:

Suppose I write a program in both Python and C and I turn these to executable. Now, will both the executable have the same speed

Of course usually not (assuming both code implement the same algorithm). And the runtime speed depends a lot of the compiler itself (e.g. tinycc -for C- and GCC or Clang ....) and even of its versions and compilation flags (e.g. -Os vs -O2 with g ). BTW, Python is compiled to some bytecode, not to machine code.

Of course, some software are mostly spending CPU time elsewhere (e.g. in some relational database manager such as PostGreSQL). Then rewriting them in C instead of Python won't gain a lot of performance. And some software are mostly IO bound (e.g. tar(1) used without compression)

At last, some C programs could generate machine code at runtime (e.g. using AsmJit...) using partial evaluation techniques, which may give a huge speedup.

On Linux, you could generate some C or C code at runtime, compile it as a temporary plugin, then dlopen(3) that temporary plugin (fetching new function pointers using dlsym(3)... Adapt the manydl.c example to your needs)

Also, C is a very difficult language to learn. Read some good book about it.

Read of course the Dragon book.

Since an entire book is needed to answer your question !

CodePudding user response:

Yes when you compiled your any code and build excitable program that's not take any impact of machine because machine understand that converted code easily just like java byte code if you have run that in any machine it's work everywhere smooth,

But If your program have different type of machine i/o calling that's make sense maybe

  • Related