Jump to content

Java "program" running faster than C

mockedarche

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

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 comment
Share on other sites

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

×