r/SoftwareEngineering • • Apr 28 '20

Simple Performance Experiment of Java, C++, and Python, on a Dell Alienware R3

I'm not sure if sharing blog posts or Medium stories on programming is allowed because it's been a very long time since I wrote a Reddit post, and this subreddit doesn't have additional rules. If it isn't please excuse me.

Anyway, this weekend, I did an experiment out of interest to measure the performance of Java, C++, and Python on my Alienware laptop with three simple functions. One that prints out "Hello world," one that does four arithmetic operations and displays the results, and one that instantiates a class and immediately destroys the object. The test bench, where I measure the execution time, uses the native timing algorithms such as the System class in Java.

This Medium story explains what I did in detail and the data I've obtained:

[Link removed because of massive criticism.]

What do you folks get when you do a similar experiment like this one? That's not to say which programming language is the best, but rather, to see what you get.

EDIT: I think I was doing this experiment for fun.

0 Upvotes

16 comments sorted by

2

u/GPD496AQ Apr 28 '20

A side comment:

Something that isn't right, though: in the test where I instantiated an object and destroyed it, the time difference Python gives me is always zero seconds.

Source code of my Python script:

import time

class PerformanceTestDataClass:
    rawInteger = 0
    rawLongInteger = 0
    rawDouble = 0.0

def helloWorld():
    print("Hello world!\n")

def mathAndDisplay():
    addResult = 5 + 5
    subtractResult = 50 - 25
    multiplyResult = 10 * 10
    divideResult = 169 / 13
    print("Math Results\nAdd: " + str(addResult) + "\nSubtract: " + str(subtractResult) + "\nMultiply: "
        + str(multiplyResult) + "\nDivide: " + str(divideResult))

def objectCreateAndDestroy():
    testObject = PerformanceTestDataClass()
    del testObject

startTimestamp = time.time_ns()
endTimestamp = time.time_ns()
timeDifference = endTimestamp - startTimestamp

for trial in range(0, 10):
	startTimestamp = time.time_ns()

	objectCreateAndDestroy() 
        // Replace above with another test function for other tests

	endTimestamp = time.time_ns()

	timeDifference = endTimestamp - startTimestamp;
	print("Time elapsed: " +
        str(timeDifference) + " nanoseconds, " +
        str(timeDifference / 1E3) +
        " microseconds, " + str(timeDifference / 10E6) + " milliseconds")

I wonder why this is happening. I'm not much experienced with Python as compared to Java and C++, so please bear with me.

What is it I can program differently with either the test bench or the objectCreateAndDestroy() function itself?

0

u/[deleted] Apr 28 '20

Python is an interpreted language, while Java and C++ are compiled languages. Python code has to be translated into intermediate instructions by an interpreter at runtime.

1

u/GPD496AQ Apr 29 '20

I'm a little curious to find out why the times for creating an object and destroying it in Python is zero.

1

u/[deleted] Apr 30 '20

The del statement removes the object name and all global references to the object when the program is interpreted, it doesn’t force garbage collection.

https://docs.python.org/3/reference/simple_stmts.html#the-del-statement

1

u/GPD496AQ Apr 30 '20

To force garbage collection, would it be gc.collect()?

1

u/[deleted] Apr 30 '20

I haven’t used it before. But I think writing some tests with the Python garbage collection interface will help with your project:

https://docs.python.org/3/library/gc.html

8

u/[deleted] Apr 28 '20

[removed] — view removed comment

1

u/GPD496AQ Apr 29 '20

I haven't tried a no-op test, but I can try it out.

9

u/[deleted] Apr 28 '20

[removed] — view removed comment

5

u/crosstherubicon Apr 29 '20

How about you cut the drama and start talking to people reasonably and constructively

3

u/[deleted] Apr 29 '20

[removed] — view removed comment

0

u/crosstherubicon Apr 30 '20

No one makes you come here.

No one makes you read the posts.

Most importantly, no one makes you reply to anything.

This is a forum for software engineers of all strengths. If its not up to your standards then find somewhere that is.

3

u/[deleted] Apr 30 '20

[removed] — view removed comment