Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simpler Java more like Python program #106

Open
igouy opened this issue Feb 13, 2023 · 2 comments
Open

Simpler Java more like Python program #106

igouy opened this issue Feb 13, 2023 · 2 comments

Comments

@igouy
Copy link

igouy commented Feb 13, 2023

The current Java program seems to have been written with error handling not found in the Python program. Instead this seems sufficient to read a single integer from a known file:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public final class leibniz {
    public static void main(String[] args) throws FileNotFoundException {        
        var s = new Scanner(new File("rounds.txt")); 
        long rounds = s.nextLong();
        s.close();
        System.out.println( pi(rounds) );
    }
    
    public static final double pi(long rounds) {
        double sum = 0.0;
        double flip = -1.0;
        for (long i = 1; i <= rounds; i++) {    
            flip *= -1.0;        
            sum += flip / (2*i - 1);               
        }                     
        return sum * 4.0;
    }  
}

rounds = 1,000,000,000

$ hyperfine "/opt/src/jdk-19.0.2/bin/java  -cp .  leibniz"
Benchmark 1: /opt/src/jdk-19.0.2/bin/java  -cp .  leibniz
  Time (mean ± σ):      4.614 s ±  0.023 s    [User: 4.630 s, System: 0.021 s]
  Range (min … max):    4.591 s …  4.645 s    10 runs

Of course, with smaller workloads the constant JVM startup becomes a factor:
rounds = 100,000,000

$ hyperfine "/opt/src/jdk-19.0.2/bin/java  -cp .  leibniz"
Benchmark 1: /opt/src/jdk-19.0.2/bin/java  -cp .  leibniz
  Time (mean ± σ):     537.2 ms ±   6.5 ms    [User: 553.2 ms, System: 16.8 ms]
  Range (min … max):   527.5 ms … 551.7 ms    10 runs

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public final class leibniz {
    public static void main(String[] args) throws FileNotFoundException {        
        var s = new Scanner(new File("rounds.txt")); 
        long rounds = s.nextLong();
        s.close();
        
        double sum = 0.0;
        double flip = -1.0;
        for (long i = 1; i <= rounds; i++) {    
            flip *= -1.0;        
            sum += flip / (2*i - 1);               
        }                     
        System.out.println( sum * 4.0 );
    }
}
$ hyperfine "/opt/src/jdk-19.0.2/bin/java  -cp .  leibniz"
Benchmark 1: /opt/src/jdk-19.0.2/bin/java  -cp .  leibniz
  Time (mean ± σ):      4.631 s ±  0.032 s    [User: 4.647 s, System: 0.020 s]
  Range (min … max):    4.597 s …  4.705 s    10 runs
@acrastt
Copy link

acrastt commented Mar 19, 2023

Using Scanner isn't efficient though, maybe RandomAccessFile could be more efficient?

@igouy
Copy link
Author

igouy commented Mar 20, 2023

isn't efficient

One integer will be read, once. How "efficient" does that need to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants