Skip to content
Advertisement

java Program runtime is too fast? Issue with Memory

So I am running some simulations that require some sample datasets. For the sake of simplicity I am using this http://loremipsum.sourceforge.net/ Lorem Ipsum generator. I am setting a test parameter called DATASIZE that sets the amount of words or paragraphs this generator creates. I am using this generated data to create an “input” and “output” hash. The output data will use a slightly different hash. For example,

String input = hash(new LoremIpsum().getWords(DATASIZE))
String output = hash(new LoremIpsum().getWords(DATASIZE-2))

My question is, does Java keep the first data set in memory and then slightly modify it to quickly produce output? Maybe I was just pessemistic on the runtime but it seems very small. Virtually zero in System.currentTimeMillis(); Could it be the jar?

I also noticed something odd with my output. I am creating several objects that store this input and output hash. On some of these that I generate, for some reason the runtime is 16. Otherwise it is 0. Something with memory or just shoddy code?

Advertisement

Answer

It uses StringBuilder. So answer to your question is NO. There is no reuse/cache in getWords(..). – https://sourceforge.net/p/loremipsum/code/HEAD/tree/trunk/src/main/java/de/svenjacobs/loremipsum/LoremIpsum.java

Having said that, if you give really large number – say 1000000 then you may see difference. I checked using my latest all powerful macbook pro

public static void main(String[] args) {
    LoremIpsum loremipsum = new LoremIpsum();
    
    long start;
    int number = 100000;
    
    for(int i=0;i<5;i++) {
        start = System.currentTimeMillis();
        loremipsum.getWords(number);
        System.out.println("getWords():" +(System.currentTimeMillis()-start));
    }

}

Output in ms

getWords():11
getWords():7
getWords():5
getWords():4
getWords():4
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement