-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

10
  • 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. Commented Oct 18, 2018 at 6:53
  • 1
    Have you tried to run all three examples as a start? Commented Oct 18, 2018 at 6:53
  • Yes there is. For linux, you can use time + some command to get the execution time Commented Oct 18, 2018 at 6: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. Commented Oct 18, 2018 at 6:54
  • I have tried it, but I can't see the difference, maybe because of small input. Commented Oct 18, 2018 at 6:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.