简体   繁体   中英

Why does this little Java program take up so much time and memory?

So I am doing online programming challenge problems so that I can become a better programmer. I am a beginner. The scope of the problem I did was to write a program that reads a number from an input stream and computes the number of zeros in the factorial of a number. The program I wrote passed the online judge and is correct. When you pass the problem it gives you data about your program. Here is my code followed by the data and my question. Here was what I came up with and wrote:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int n = -1, count = 0;

        //first number is an int that tells us how many numbers are in the input
        //(how many numbers we need to run the algorithm on)

        Scanner sc = new Scanner( System.in );
        int bound = sc.nextInt();

        //for each number in the input
        for (int i = 0; i < bound; i++) {

            n = sc.nextInt(); //read the number

            count = 0; //set number of 0's in that factorial to 0

            int multipleOfFive = 5; //start counting number of multiples of 5

            while (n / multipleOfFive > 0) {

                //add the number of times this multiple of 5 goes into the number

                count += n / multipleOfFive; 

                multipleOfFive *= 5; //get next factor of 5

            }

            System.out.println(count); //print this numbers solution

        }

        sc.close(); //close the scanner
    }
}

The system came back with the following data on my program:

memory usage: 5512k, time: 1375ms

I compared this with the other users who had correctly submitted this problem and they were in these ranges:

memory usage: 144k - 228k, time: 125ms - 625ms

I was kind of surprised at how different the times were. I don't know how I can cut my time down that much. So my question is this... Is there anything obvious I can do to improve this program to obtain a lower time and lower amount of memory usage? Is using the Scanner class a bad idea? I tried researching different i/o classes and did not find anything that looked very promising to improve either space or time. If anyone has any helpful suggestions say so. To clarify, the question is not about how the algorithm works. The question is about how to make it better. Thanks.

Update: They can write code in C, C++ or Java. So I guess part of my question is this: would doing something as simple as writing the exact same thing in C or C++ be enough to cut the runtime in half (assume on the same computer) and/or would the elimination of a virtual machine be enough to significantly cut down on the memory usage? And to clarify, the data I listed is how much memory the program used and the total time in milliseconds it took to execute.

So now that I see what you're comparing your numbers against (C/C++) there is very little you can do to reduce what you're seeing.

Java is different from C/C++ in that it spins up a complete Java Virtual Machine (JVM) every time you execute the application. This alone will incur more memory requirements and consume start up time. It's a fact of Java, but one that isn't generally an issue given the massive memory of most machines and the fact that most Java apps are long running processes. For such a long running process, incurring a few extra seconds of startup time is a non-issue.

On the upside, your compiled jar file will run on any OS that has a JVM, whereas the compiled C/C++ apps don't have that capability, and need to be compiled specifically for each platform/OS combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM