Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Ohsnaps

Java "program" running faster than C

Recommended Posts

Posted · Original PosterOP

Long story short I got bored and decided to type out a simple java idk script or whatever that calculates prime numbers. Simple enough just wanted to do it and then try and optimize stuff as far as I could. Didn't get far into optimizing and am not amazing coder yet but I know the syntax of Java and C. Figured i'd copy it over since i've heard that C is faster than Java. When I run both (same time or just time it when its running) Java proceeds to get further faster. Im not quite sure why am assuming its either because how I copied it made it rather inefficient in C or because Java has better multi threading? Or maybe C's way of printing to console is slower? IDK figured either way I'd learn something by asking why it is here. (Please be polite remember i'm definitely no programmer). Note that the large number inside the while wasn't meant to be reached rather just there to allow easy execution (don't gotta enter anything).

 

C.png

java.png

Link to post
Share on other sites
2 minutes ago, Ohsnaps said:

or because Java has better multi threading?

Neither C or C++ do multi-threading, unless you explicitly program multi-threading into the app yourself. I have no idea about Java.


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Can you comment/fix indenting on your code so that it's a little easier to understand? I'm not sure I'm 100% following what lines up where, and it'll help you figure out, for example, if there's things happening multiple times that only need to happen once.

Link to post
Share on other sites

First things first:

 

  1. C and Java work with explicit multithreading. So that's not it.
  2. Java compiler applies optimizations by default and the virtual machine uses Just-In-Time (JIT) compilation to speed things up. That means it can see your loop and use vector instructions (SSE, MMX, AVX, ...) to speed things up.
  3. C/C++ compilers do not apply optimizations by default and there's no JIT.

What does that mean? You probably missed a crucial step of passing optimization options for your compiler.

If you're using GCC, try "gcc -o prime.exe -O3 -march=native prime.c".

 

P.S.: You're correct on assuming C printing is slow. If you prefer, use a C++ compiler, replace print with std::cout calls, and put "std::ios_base::sync_with_stdio(false);" at the beginning of the main function. It should be screaming fast.

Link to post
Share on other sites

What's going on here is a difference of compiler optimizations, and the fact that you are including the time required to write text to the screen in your benchmark.

Here are the usual next steps for this task:

  1. Separate your code out to three sections
    1. Input gathering, which won't be benchmarked
    2. Generating prime numbers, which will be benchmarked
    3. Outputting the results, which won't be benchmarked.
  2. Make sure that you give your C code an equal chance
    1. If you have optimizations enabled in your Java compiler (they are unless you've changed this), make sure you set the same settings in your C compiler.
  3. Use a Sieve of Eratosthenes.
  4. Notice that your C code is now faster.
  5. Optimize both of the sieve algorithms (there are a few things you can do here, such as noticing that you only actually have to check the numbers below Ceiling(SquareRoot(MaxNum)).
  6. Optimize your C code to use pointers instead of array indexing.

I will never succumb to the New Cult and I reject the leadership of @Aelar_Nailo and his wicked parrot armies led by @FakeCIA and @DildorTheDecent. I will keep my eyes pure and remain dedicated to the path of the One True; IlLinusNati

Link to post
Share on other sites
13 hours ago, Ohsnaps said:

<snip>

The C program is broken. Both i and j can reach a value of 99999. Multiplying them would overflow int. signed int overflow is undefined behavior.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×