-2

I have a question that I think it is a stupid question maybe. So if we have an algorithm, assume it recursion algorithm, but we implement it with different programming languages, is there any performance difference between implementations? For example are from these sample code.

void printFunInCpp(int test) 
{ 
    if (test < 1) 
        return; 
    else
    { 
        cout << test << " "; 
        printFun(test-1);    // statement 2 
        cout << test << " "; 
        return; 
    } 
}

static void printFunInJava(int test) 
{ 
    if (test < 1) 
        return; 
    else
    { 
        System.out.printf("%d ",test); 
        printFun(test-1); // statement 2 
        System.out.printf("%d ",test); 
        return; 
    } 
}

def printFunInPython(test): 

    if (test < 1): 
        return
    else: 

        print( test,end = " ") 
        printFun(test-1) # statement 2 
        print( test,end = " ") 
        return

So, from example above, is there any performance difference within the 3 programming languages? If there is a performance difference, is there any technique to know it? How about memory usage?

Thanks

dev-x
  • 897
  • 3
  • 15
  • 30
  • 2
    There is difference because language do same but compiler work differently. As far as I know C++ work very fast at most of times and python and java slower (specially java because use JVM) I don't know there is a good way to know which one is better because in different aspect, they work differently. you can get duration of your app with some code in each language, but this is not a good way. – Amin Oct 18 '18 at 06:53
  • 1
    Have you tried to run all three examples as a start? – Mad Physicist Oct 18 '18 at 06:53
  • Yes there is. For linux, you can use `time` + some command to get the execution time – Kevin Fang Oct 18 '18 at 06:53
  • 3
    Yes, because C, Java and Python operate on different levels of abstraction. C is lightning fast but offers little abstractions to make your life as a programmer easier. Python is super convenient but much slower. `for` loops in Python invoke the whole object oriented machinery of method calls and iterators, for example. – timgeb Oct 18 '18 at 06:54
  • I have tried it, but I can't see the difference, maybe because of small input. – dev-x Oct 18 '18 at 06:56
  • When using `time` you should call the function a lot of times in the loop. Otherwise you just measure the start-up time of your program. – Guido Flohr Oct 18 '18 at 06:56
  • Also, keep in mind that Java deploys an JIT compiler, thus you need some burn-in for the JIT to take effect. – Turing85 Oct 18 '18 at 06:59
  • Yes, there is performace differences. Someone correct me if I'm wrong, but C, in example, when compiled, is converted into binary that can be understood by the machine. There is few steps between the execution and the hardware. Java or C#, when compiled, have to be executed by a runtime, which is executed by the system. There is more steps between this binary and the hardware, hence it's slower – Cid Oct 18 '18 at 07:09
  • There is no C code in the question. – Antti Haapala -- Слава Україні Oct 18 '18 at 07:20
  • Keep in mind that writing to the console is likely to be 10,000x slower than your code so what language you use hardly matters. – Peter Lawrey Oct 18 '18 at 07:31

2 Answers2

3

Yes, there's a performance difference and it depends on many different factors. The C code will likely be the fastest because it's compiled directly into machine code for your computer's architecture, it doesn't get much faster than that.

The Java code will be compiled into machine code for its own virtual machine. That is bound to be slower than native machine code, although it has improved a lot in the past years. What's really going to drag down Java in this example is the overhead (starting the JVM and so on, it can require about 35 MB for something as simple as a "Hello World!"). That alone is not going to be a big factor for a program running for a long time, but for a short program that terminates in a few miliseconds, it will.

Python is an interpreted language. The Python program will have to be turned into machine instructions on the go. That will, of course, impact its performance, in exchange of other advantages.

In short, different languages use vastly different concepts that have different trade-offs. Performance is one of those thing that can be traded off for other advantages. It's important to use the right tool for the right job, and for some jobs you'll want high performance while for other jobs convenience, fault tolerance, compatibility or something else might be preferred.

Blaze
  • 16,736
  • 2
  • 25
  • 44
1

There is already a discussion on this:

Performance differences between Python and C

Just between C and Python.

In general, it is hard to say. In python, a lot for modules are written in C to increase the execution performance. The benefits of python by design is more on the easiness of writing and reading code and not in the execution time.

Hope it helps a bit.