r/learnjava • • Nov 03 '20

Why is c faster than java?

If I were to make the same thing in java would it now be as fast ?

58 Upvotes

26 comments sorted by

View all comments

2

u/Gixx Nov 04 '20 edited Nov 04 '20

See this timestamp on youtube interviewing Chris Lattner (creator of Swift and LLVM).

Lattner says, "I wanna write one program, and have it portable. This is something that assembly doesn't do."

So while you think C might be a god-tier language you need a compiler or thing that translates it to an architecture.

  • x86

  • ARM

  • RISC-V, ASIC

  • MIPS

I'm learning about it, and don't know a lot about low-level information.

x86 instruction sets can only be made by AMD and Intel. ARM can only be made by the company ARM (now sold to Nvidia).

To hear more above what I'm writing watch that entire youtube video (interesting timestamps: 572, 4000, 4538)

I'm also interested in performance. So I did 43 coding problems on Kattis.com in java. Now I've done 7 problems in Go.

Why is Go much faster than Java? Go compiles to a single binary, while java can run on any architecture that has a JRE installed. But kattis penalizes java because it includes the JVM startup time (the site does not say this, but the rank 17 guy (Bjørn Dons) told me Java should be just as fast as C++ if it didn't include the JVM load time).

A list of my memorable or interesting problems can be found here.

problemruntime Java (sec)runtime Go (sec)
helpaphd0.150.02
haypoints0.330.08
heartrate0.350.08
guessthedatastructure0.560.06
goldbach20.530.08
backspace0.230.10
roundedbuttons0.790.10