-1

I am trying to crate a code, which calculates Java Map's containsKey -operation in nanoseconds (like how fast it runs that operation). Method has a Map<Double, Double> D as a parameter, and the value returned should be containsKey operations' normal use of time as nanoseconds. I have really no clue, how to continue this. Netbeans IDE gives error message, which says that I have some kind of infinite loop here.

Here is my code so far:

import java.util.*;


public class calculateNanotime implements calculateNanotime{

   /* 
     * Measures Map's containsKey -operaation's time frequency as nanoseconds.
     * @param D Map to be tested
     * @return containsKey -operation's duration as nanoseconds
     */

    //@Override
    public long containsKeyTime(Map<Double, Double> D) {

        // I should try with different key values
        D.containsKey(1.0);
     
        long beginning = System.nanoTime();
        long end = System.nanoTime();
        
        long result = end - beginning;
    
 //I have a code in another class of this package, which will be testing how well this code work, after this is ready
        result = testable.containsKeyTime(D);
          return result;
}
}

Here are the erroe messages: Exception in thread "main" java.lang.StackOverflowError at calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:35)

at calculateNanotime.calculateNanotime.containsKeyTime(calculateNanotime.java:42)

  • 1
    What is this testable? – Gaurav Jeswani Nov 04 '20 at 15:27
  • 2
    If you have an error message then it is useful to include this in your post. – selig Nov 04 '20 at 15:32
  • @notescrew There is a separate class in my package,which tests the usability/working of the code in class calculateNanotime. – QuestionsAndAnswers Nov 04 '20 at 15:38
  • 1
    First I would remove this line: result = testable.containsKeyTime(D); second I would move the D.containsKey call between the two System.nanoTime measurement. Also what you trying to do is microbanchmarking.Suggest you to read about this as it is a bit harder problem to solve. – András Tóth Nov 04 '20 at 15:46
  • 1
    You should be aware that micro-benchmarks such as this in Java involve some subtleties that can lead to misleading results, e.g. are you trying to measure the time it takes before or after the JIT compiler kicks in? The compiler might also elide the operation entirely if it detects that the output is not being used. JMH is the recommended tool to write such benchmarks: https://github.com/openjdk/jmh – MikeFHay Nov 04 '20 at 15:56
  • @MikeFHay I am trying to measure time after JIT compiler kicks in. – QuestionsAndAnswers Nov 04 '20 at 16:03
  • ^^^ good luck with that – Amir Afghani Nov 04 '20 at 19:40
  • 1
    Related: [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Ole V.V. Nov 05 '20 at 04:09
  • Could you please [create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? It seems to me that the answers posted so far have been guesswork because based in the information posted so far, we can’t be sure what’s really gone wrong. I am downvoting your question and voting to close as unclear. – Ole V.V. Nov 05 '20 at 04:12

2 Answers2

2

Your infinite loop error is likely to come from this

public class calculateNanotime implements calculateNanotime

where you have a class implementing an interface of the same name.

See Java Class and Interface Name Collision

selig
  • 4,834
  • 1
  • 20
  • 37
1

You don't have an infinite loop, what you have is a recursive function with no base case to terminate. Your containsKeyTime method should probably take as a parameter the value you want to check if the map contains as a key. Maybe something like :

public class calculateNanotime implements calculateNanotime {    
    @Override
    public long containsKeyTime(Map<Double, Double> doubleMap, double value) {     
        long beginning = System.nanoTime();
        boolean result = doubleMap.containsKey(value); //what to do with result?
        return System.nanoTime() - beginning;
    }
}
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124