Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to know why Java is accepted over Python when it comes to runtime. On performing the following Euler problem, Python takes up less lines and runs faster (Python ~0.05s, Java ~0.3s on my machine).

Could I optimize this Java code in any way? The problem is here (http://projecteuler.net/problem=22)

Python:

def main():
    names = open("names.txt", "r").read().replace("\"", "").split(",")
    names.sort()
    print sum((i + 1) * sum(ord(c) - ord('A') + 1 for c in n) for i, n in enumerate(names))

if __name__ == "__main__":
    main()

Java:

import java.util.Arrays;
import java.lang.StringBuilder;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class euler22
{
    public static String readFileAsString(String path) throws IOException
    {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(
                new FileReader(path));
        String buffer = null;

        while((buffer = reader.readLine()) != null)
        {
            builder.append(buffer);
        }

        reader.close();

        return builder.toString();
    }

    public static void main(String[] args) throws IOException
    {
        String[] names = fileAsString("names.txt").replace("\"", "").split(",");
        int total = 0;

        Arrays.sort(names);

        for(int i = 0; i < names.length; ++i)
        {
            int sum = 0;

            for(char c : names[i].toCharArray())
            {
                sum += c - 'A' + 1;
            }

            total += (i + 1) * sum;
        }

        System.out.println(total);
    }
}
share|improve this question
2  
How exactly are you compiling your java code? Try deleting the pyc file and timing the Python piece twice in a row. Also, time the io-bound piece and the cpu-bound piece for Python and Java separately. The computation is in theory parralelizable, but not in Java version. I do not know enough to be sure though. I suggest that you use with open('file.txt', 'r') as fin: names = fin.read()... for better safety. You also probably can speed up the Java reading piece a bit if you tokenize things manually. Java's and Python's io library must differ, and one is taking more advantage of hardware maybe –  Leonid Apr 19 '12 at 4:07

1 Answer 1

So, what exactly do you want to know, what is your question? I do not think you have to refactor anything here. Maybe you can push the limits, but first read this (and you should know why java is choosen):

  1. Are you counting just the time of program execution (your code does not show this), or the total time of jvm startup, program execution and jvm shutdown (the same for python startup, exec, shutdown). You have to remember that this time differs between different interpreters/jvm's. And it can influence your computation. For such a small problem it will be the main factor.

If you have a bigger problem, that runs for a longer period of time, then the jvm startup does not matter. If your program has to run for a month it does not matter if jvm starts in 1 ms, 1 second or even 1 hour.

Be sure that you know what you want to measure.

  1. Java starts by interpreting, which is not the fastest way of runnig programs, and after some time, and hot-spots analysis the Java HotSpot JIT compiler kicks in, and the you will see that the program speeds up.

I have taken your example, and run it a few times (counting the time of each execution). It starts from about 130ms, and after a few cycles it runs in less than 8ms. And I know that file read and access time is a factor also, but after first read the file should be in Operating System cache.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.