# How to calculate 95th percentile in Java

• 04-22-2013, 02:02 AM
raihan26
How to calculate 95th percentile in Java
I am trying to calculate 95th Percentile from the data sets which I have populated in my below ConcurrentHashMap.

My Map will look like this- In which

Code:

```    key - means number of milliseconds     value - means number of calls that took that much milliseconds```

Code:

```    Milliseconds    Number         0              1702     1              15036     2              14262     3              13190     4              9137     5              5635     6              3742     7              2628     8              1899     9              1298     10              963     11              727     12              503     13              415     14              311     15              235     16              204     17              140     18              109     19              83     20              72```

For example, from the above data sets, it means

Code:

``` 1702 calls came back in 0 milliseconds  15036 calls came back in 1 milliseconds```
Now I can calculate the 95th percentile by plugging the above data sets in the `Excel sheet`. But I was thinking to calculate the percentile in Java code.

I know the algorithm will look something like this-

Sum all values from your map, calculate 95% of the sum, iterate the map keys in ascending order keeping a running total of values, and when sum equals or exceeds the previously calculated 95% of the total sum, the key should be the 95th percentile.

But I am not able to plugin this algorithm in the Java code. Below is the map which will have above datasets.

Code:

`Map<Long, Long> histogram = new ConcurrentHashMap<Long, Long>`
I am not sure what is the best way to calculate the percentile in Java. I am not sure whether I am algorithm is also correct or not.

Code:

```    private static void calculatePercentile() {                 for (Long time : CassandraTimer.histogram.keySet()) {                             }         }```
Can anyone provide some example how to do that?

Any help will be appreciated.
• 04-22-2013, 02:53 AM
kjkrum
Re: How to calculate 95th percentile in Java
Quote:

Originally Posted by raihan26
I know the algorithm will look something like this-

Sum all values from your map, calculate 95% of the sum, iterate the map keys in ascending order keeping a running total of values, and when sum equals or exceeds the previously calculated 95% of the total sum, the key should be the 95th percentile.

That is completely wrong. See Percentile - Wikipedia, the free encyclopedia.
• 04-22-2013, 03:04 AM
raihan26
Re: How to calculate 95th percentile in Java
I see. Yeah it looks like, I am wrong. Can you provide me an example how to calculate the 95th percentile for my question? Thanks for the help.
• 04-22-2013, 05:13 AM
willemien
Re: How to calculate 95th percentile in Java
just count the >> numbers << together
0.95 times that number.
then look at the time that that sample took
so in your case , there were around 70.000 calls (quick count) so what time took the 66500 (shortest call?