C versus Java - Performance Test (Fibonacci of the first 50 integer numbers)

,

Maybe you should do more of these.
Lines of Code would be another metric I would be interested in seeing explored.

2 Likes

Also kotlin version of the same thing

fun main() {
    val start = java.lang.System.currentTimeMillis()

    for(i in 0.toLong()..49)
        println(fibonacci(i))

    val sec = (java.lang.System.currentTimeMillis().toDouble() - start) / 1000
    println("The program took around $sec seconds to calculate the fibonacci of the first 50 numbers.")
}

fun fibonacci(n: Long): Long {
    if (n <= 1) {
        return n
    }
    var fib: Long = 1
    var prevFib: Long = 1

    for (i in 2 until n) {
        val temp = fib
        fib += prevFib
        prevFib = temp
    }
    return fib
}

I’m not sure if you noticed, but your time conversion was wrong. I copy pasted it from your code xD. You / 1000 milliseconds to get seconds not / 100. So this result you would have to * 10 to compare it to the earlier results.

1 Like

Good catch on the time conversion. My eyes never caught that.

1 Like

So C runs bad code faster than Java? Why don’t you write a decent implementation of fib and run it as a server? That would be at least a slightly realistic scenario. All you’re testing is function call overhead in a short lived program. Java’s VM needs to run for a while to warm up. Try writing a server that takes an HTTP request and runs a good fib (something that can handle big numbers) and returns a JSON encoded response. Maybe use apache bench?

When your programm takes more than 200 seconds to get up to speed, you got some bad programm on your hands.

2 Likes

True it’s definitely warmed up by then. That wasn’t really the point. My point is that this test is looking at such a small and relatively boring execution path that it’s not remotely convincing. And full disclosure I develop in C professionally and don’t particularly like Java. This is just a boring test.

Come up with a different algorythm to test then (maybe make a thread about it?).

I don’t have to come up with a different algorithm, it’s been done already. Not hard to find fast Fibonacci algorithms if you just look.

https://www.nayuki.io/page/fast-fibonacci-algorithms

Here you go, they even come with a Java implementation. Since people in this thread love C so much, I’ll leave that as a challenge :wink:

IMPORTANT STUFF:

Hey everybody I’m truly sorry but I don goof’d :frowning:

I made a rookie mistake at converting MS to S and then it looked like Java had ran slower than it actually did.

Long story short: Java outperforms standard compiled C code but still looses to Optimized Flagged C Program.

I tried it on my desktop this morning and when I took a glance at the code I panicked… there was a missing 0 on the java code, so here are the results changing the LONG to INT on Java, C (standard and sanic) and C++ (also standard and tails[-O2]):

The OS and tools are the same as the laptop, what differs is the CPU and amount of RAM: i3 3240 @ 3.4 gHz and 8 gigs of 1333 DDR3 RAM.

This is the Java one, with the right conversion.

This is the Standard Flag C program.


This is sanic compiling in C (still my favorite)

I also tried with C++:

Standard flag.

This is the -O2 flag (my main man tails compiling)

So: Java is slower than optimized C and C++ code. Maybe with JVM tuning we could get somewhat similar results (although I don’t think it would win).

I edited the OP so new people might understand better. I’ll reply you guys soon enough.

1 Like

That is some well written code, that was nice, hadn’t think of that.

How can we fix that? What do you suggest as a good number crunching program? I’m interested and all in.

Will do, my friend. Also in the next iterations I’ll try to optimize the code the best way possible.

YES! I realized it just today morning, already edited the OP and posted some other results at the end of the post… I’m sorry :t

Mostly cause I can’t do it. If you could that would be a nice addition to the post and would be benefitial to everyone.

I actually agree with you on this one hahaha

Things have taken such a weird turn… I loved it.

2 Likes

That’s what happens when you say “insert language, distro, anime, movie” is better than another to a large group of nerds…

2 Likes

Yeah, basically.

But it was fun.

I’m actually confused right now… I really thought java would be sluggish as hell. Will need therapy.

2 Likes

Java is actually really fast for todays hardware. it has many benifits that something like C or basic would not have. It also doesn’t have the same type of security concerns that the other languages have. Now I’m not saying there are no security concerns, just different ones. That’s why I’m trying to say, “Girls, you’re both pretty!”

Either bring forth your own brains work backed up by sources or don’t argue. Throwing links is too cheap.
Hope the popcorn is good :smiley:

Don’t get me started on Forth.

1 Like

I’m truly sorry for messing up the conversion and bringing false results on the OP, but I still think the “performance test” is a fairly reasonable question to consider.

It is indeed a bad implementation of fibonacci, but it was implemented the same across all platforms (except for that c++ in which I slapped integers instead of longs, but then I corrected everyone).

Each run was fair to the ones posted together.

And the thing is: Java is ok, not great not terrible, but you won’t run it flawlessly on “weak” systems such as embedded ones and microcontrollers, while C will, since to run it you just need the executable and an OS (probably, I’m not 100% sure), while with the other one you’ll need JRE.

At the end of the day it’s all about the right tool for the right job, although I think it would be nice running native applications on backend tasks…

Please start Forth, I’m curious.

Dude, no, don’t apologize for making a thread and attempting to get people engaged.

7 Likes

Interesting thread. Great idea OP :fist_right::fist_left:

2 Likes

Oh no, I’m sorry for the conversion error, not for the OP or the idea itself.

Problem is the conversion error brought wrong data to the table, data an extra “0” haha

1 Like