Question

I'm making a performance test about search in graphs (between an adjacency list and adjacency matrix) to calculate average traverse time for whole graph in ms.

However, the output gives a number that I havent seen before. Here is the simple algorithm of test.

   double startTime;
   double endTime;
   double processTime;

   double totalTime = 0;
   double averageTime = 0;

    for (int i = 0; i < 100000; i++) {


        startTime = System.nanoTime();

        search.DFS(5);

        endTime = System.nanoTime();

        processTime = (endTime - startTime)/1000000;

        totalTime = totalTime + processTime;
    }

    averageTime = totalTime/100000;

    System.out.println("PROCESS TIME in AdjacencyMatrix = " + averageTime + " ms");

and the output looks like ;

PROCESS TIME in AdjacencyMatrix = 1.4765902999997995E-4 ms

When I traverse just one time the output gives convenient data like 0.032344 ms.

Was it helpful?

Solution

Even if you use double you still can get precision errors. Basically a floating point number is always a sum of numbers to the power of ten. With that means not all number can be saved precisely. I'm guessing that's where your problem lies.

As a solution: Use long for your variables which is the return type of System.nanoTime(). The number returned by this is always a whole number.

OTHER TIPS

The number is simply written as a floating point number.

1.4765902999997995E-4 = 0.000147659...

So because it's a very small number, the output is in float format to be easier to read.

EDIT: To clarify about floating point number. It's just a way to write numbers that is usually referred to as "scientific notation". This is just a different and convenient way to write very large or very small numbers. In school you might've run into something like:

3,000,000 = 3 * 10^6 = 3E6

1,456,000,000,000 = 1.456 * 10^12 = 1.456E12

So the "E" simply means *10^. If the numbers are small, you use negative numbers after the E, like this:

0,04653 = 4.653 * 10^-2 = 4.653E-2

0,00000134 = 1.34 * 10^-6 = 1.34E-6

0.000000000000000018 = 1.8 * 10^-17 = 1.8E-17

If you want more examples, you can the box in the top right corner of this wikipedia article

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top