Well hello there, fellas, are you in for a nice ride along computer mathematical applications performance tests?
If no then you can go away 'cause we about to get kinda technical here. Here’s my entry on the “Just Do It Challenge”.
First I’ll link a Wikipedia article on Fibonacci to give you an intro: Fibonacci - Wikipedia
So yeah, long story short: you calculate the numbers based on the golden ratio.
Then let me tell you about two programming languages:
One is an oldie but certainly golden, we call it C, invented by Dennis Ritchie between 1972 and 1973 at Bell Laboratories to run on UNIX systems (according to wikipedia, you can check it out if you want), that compiles into binaries that are read by the machine (some people here might give you a better explanation, or even duck it yourself).
The other one is Java, developed initially by James Gosling at Sun Microsystems (later Oracle) and released at 1995 (it’s younger than me), that compiles into binaries that are read by a Virtual Machine (Java Virtual Machine - JVM) on top of the OS.
Basically C is praised for its performance and Java despised by the lack of it. There is a thread here in the forum to mock one of them.
I have just a little knowledge about both of these languages, I’m being honest, I know very little, but enough to pull out a program that will calculate and display the Fibonacci sequence from 0 to 50 (plus a \n [new line]) at the end and tell us how much time it spent doing just that. It is a recursive function inside of a “for” loop.
Before you ask “why 50?” I tell you: “because of time, my dear”. I tried calculating 100,000, then 1,000 and yet 100… it took way too long and I have other important stuff to do, this program is way heavier than I thought.
Here is the code for the C program:
#include <stdio.h>
#include <time.h>
long fibonacci (long x)
{
if (x == 0)
return 0;
else if (x == 1)
return 1;
else
return fibonacci(x-1) + fibonacci(x-2);
}
int main()
{
clock_t t;
long i;
t = clock();
for (i = 0; i <=50; i++)
{
printf("%lu\n", fibonacci(i));
}
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("It took %f seconds to finish the program from 0 to 50.\n", time_taken);
return 0;
}
Yeah, it looks cool, right? And here’s the code for the Java program:
class Fibonacci
{
public static void main(String[] args)
{
double startTime = System.currentTimeMillis();
for (long i = 0; i <= 50; i++)
{
System.out.println(fibonacci(i));
}
double stopTime = System.currentTimeMillis();
double elapsedTime = (stopTime - startTime) / 100;
System.out.println("The program took around " + elapsedTime + " seconds to calculate the fibonacci of the first 50 numbers.");
}
static long fibonacci(long x)
{
if (x==0)
{
return 0;
}
else if (x==1)
{
return 1;
}
else
{
return fibonacci(x-1) + fibonacci(x-2);
}
}
}
These brackets for 1 line instructions might not be necessary but I stuffed’em there anyways, I’m too tired already to try it <3
Now for the system I used to test both:
Dell Inspiron n4050, i5 2450m with 4 gb of ram @ 1333 mHz, plugged into energy for standard boost.
OS: GNU/Linux Mint 19.2 Tina, updated just a few minutes before the tests, so we pretty fine.
GCC version: 7.4.0 (got it by downloading build-essential, cause vanilla mint has some issues with gcc)
Java version: OpenJDK 11 (Javac 11.0.4 - Java 11.0.4)
And into the long awaited results:
Java with the standard Javac -> 2,200.73 seconds to finish.
[EDIT_1] Guys I’m sorry but I made a huge mistake, it’s actually 220 seconds, if you take a look at the conversion on the code I forgot to add another 0, that means the conversion is WRONG, PLEASE PARDON ME! [/EDIT_1]
C time without optimization flag: 396.616303 seconds to finish.
C with the flag “-Ofast” (felt like sanic the herdgehog): 141.472849 seconds to finish (boy it was fast).
LONG STORY SHORT: Yeah, the memes are right, Java is fairly slower than C on demanding applications.
[EDIT_2] Considering the right time conversion to Seconds in JAVA it actually means a standard C program will be outperformed by a Java one, but the OPTIMIZED C PROGRAM WILL PERFORM BETTER [/EDIT_2]
Both applications let the CPU at around 30% and memory at 25%. Now for the sizes of the files:
Java text: 540 bytes
Java Class (the application itself): 1.2 kb
C text: 424 bytes
C standard compiling: 8.4 kb
C with sanic compiling (which I named fibonacciTurbo btw): 13.9 kb
There are a few reasons, but I won’t get too technical on the OP, but basically is the JVM thing that runs above the OS and the Garbage Collector (probably, I’m no expert).
But there you have the code, feel free to try it yourself, specially if you have some beefier cpu/system.
I could study multithreading to try and optimize both even more, but I won’t right now… maybe later… maybe not… IDK
You’re waiting for the potato, right?
Here, have it:
Thanks for your time and feel free to correct me, yell at me, cuss, anything. But remember I’m no expert.
[EDIT_3]The OP was edited because I made a rookie mistake on time conversion and forgot to add another dividing ms to s. SORRY! [/EDIT_3]