热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将数字拆分为不同的范围-Splitthenumbersinadifferentrange

Iamtryingtomeasurehowmuchtimeeachthreadtakesininsertingtodatabase.Ihavecapturedall

I am trying to measure how much time each thread takes in inserting to database. I have captured all those performance numbers in a ConcurrentHashMap named map like how much time each thread is taking in inserting. In that concurrent hash map it will be something like this.

我试图测量每个线程插入数据库所花费的时间。我已经在ConcurrentHashMap命名映射中捕获了所有这些性能数字,比如每个线程插入的时间。在那个并发哈希映射中,它将是这样的。

Key- 10
Value- 2

So that means, 2 Calls came back in 10 ms. Another example below

这意味着,2个呼叫在10毫秒内恢复。另一个例子如下

Key - 20
Value -1

which means, 1 Call came back in 20 ms.

这意味着,1个电话在20毫秒内回来了。

And that map will contain lot more data means lot more key value pair.

而且该地图将包含更多的数据意味着更多的关键值对。

So now I am trying to do something like below by using the same map above, that means I need to iterate the above map to get the below numbers in that particular range. Is that possible to do?

所以现在我尝试通过使用上面的相同地图来做类似下面的事情,这意味着我需要迭代上面的地图以获得该特定范围内的以下数字。这可能吗?

How many calls(X number) came back in between 1 and 20 ms
How many calls(X number) came back in between 20 and 40 ms
How many calls(X number) came back in between 40 and 60 ms
How many calls(X number) came back in between 60 and 80 ms
How many calls(X number) came back in between 80 and 100 ms
How many calls(X number) came back after 100 ms

Some code that I thought of initially.

我最初想到的一些代码。

SortedSet keys = new TreeSet(map.keySet());
for (Long key : keys) {
    System.out.print(key + " : ");
    for (int i = 0; i 

Can anyone help me here?

有人能帮我一下吗?

Update:-

My map sample value-

我的地图样本值 -

{31=3, 48=1, 33=1, 30=12, 43=1, 38=1, 32=1}

It means total call was 3+1+1+12+1+1+1 = 20 by adding value from the map

这意味着通过从地图添加值,总呼叫为3 + 1 + 1 + 12 + 1 + 1 + 1 = 20

And out of that I need to figure out the above scenario means something like this

除此之外,我需要弄清楚上面的情况意味着这样的事情

How many calls(X number) came back in between 1 and 20 ms
How many calls(X number) came back in between 20 and 40 ms
How many calls(X number) came back in between 40 and 60 ms
How many calls(X number) came back in between 60 and 80 ms
How many calls(X number) came back in between 80 and 100 ms
How many calls(X number) came back after 100 ms

Below is my code that I have tried with the below suggestion-

以下是我用以下建议尝试过的代码 -

private static void drawHistogram(Map map) {

private static void drawHistogram(Map map){

int counter[] = new int[6];

for (Integer key : map.keySet()) {
    System.out.println("" + key);    

    // add sample
    int idx = key / 20;
    idx = Math.min(idx, counter.length - 1);
    counter[idx]++;
    }

for (int i = 0; i 

}

As you can see, I have 20 calls made but this is showing only 7 calls. Anything wrong I did? This is the output I got-

如你所见,我有20个电话,但这只显示7个电话。我做错了什么?这是我得到的输出 -

0 came back in between 0 and 20 ms
5 came back in between 20 and 40 ms
2 came back in between 40 and 60 ms
0 came back in between 60 and 80 ms
0 came back in between 80 and 100 ms
0 came back in between 100 and 120 ms

which shows only 7 calls. But there are 20 calls.

它只显示7个电话。但是有20个电话。

5 个解决方案

#1


0  

Anticipating the need to easily redefine bucket size (and indeed the number of buckets you aggregate into) I propose:

预计需要轻松重新定义铲斗尺寸(实际上是您聚合的铲斗数量)我建议:

    Map values = new HashMap();

    int[] definition = {0, 20, 40, 60, 80, 100};
    int[] buckets = new int[definition.length];

    for (int time : values.keySet()) {
        for (int i=definition.length-1; i>=0; i--) {
            if (time >= definition[i]) {
                buckets[i] += values.get(time);
                break;
            }
        }
    }
    for (int i=0; i

Configurability is managed by changing definition. I used the following code to test this:

通过改变定义来管理可配置性。我使用以下代码来测试这个:

    Random rnd = new Random();
    for (int i=0; i<1000; i++) {
        int time = rnd.nextInt(121);
        Integer calls = values.get(time);
        if (calls == null) {
            calls = Integer.valueOf(0);
        }
        calls += 1;
        values.put(time, calls);
    }

#2


0  

SortedSet keys = new TreeSet(map.keySet());
Map values = new HashMap();
Integer total = null;
Integer current = null;
Long point = null;
for (Long key : keys) {
    System.out.print(key + " : ");
    current = map.get(key);
    if(key >= 1 && key <= 20) {
        point = 1;
    } // Do Other Comparisons also and change point 2, 3, 4, 5, 6

    total = values.get(point);
    if(total == null) {
        total = 0;
    }
    total += current;
    values.put(point, total);
    System.out.println();
}

Now if you loop throw the keySet of values

现在,如果你循环抛出值的keySet

Point 1 will be How many calls(X number) came back in between 1 and 20 ms

第1点将是在1到20毫秒之间回来的多少次呼叫(X号码)

#3


0  

You may try:

你可以尝试:

SortedSet keys = new TreeSet(map.keySet());

int group1To20=0;
int group20To40=0;
int group40To60=0;
int group60To80=0;
int group80To100=0;
int groupAbove100=0;
for (Long key : keys) {
 if(key>=0 && key<=20){
  group1To20=group1To20+map.get(key);
  }elseif(key>20 && key<=40){
   group20To40=group20To40+map.get(key);
  }
 //Similarly do as above for other range of group

}//end of loop


System.out.print("group 1-20 contains " +  group1To20);
//Now print the group range and values here

}

I have tried for your solution. I may misunderstand your question. If so, then clearify the question for me.

我试过你的解决方案。我可能会误解你的问题。如果是这样,那么请为我澄清问题。

#4


0  

You could use a NavigableMap which allows you to query a range of numbers (head, tail). A thread safe implementation is ConcurrentSkipListMap.

您可以使用NavigableMap来查询一系列数字(头部,尾部)。一个线程安全的实现是ConcurrentSkipListMap。

In particular look the the methods NavigableMap headMap(K toKey, boolean inclusive), NavigableMap tailMap(K fromKey, boolean inclusive) and SortedMap subMap(K fromKey, K toKey)

特别是查看方法NavigableMap headMap(K toKey,boolean inclusive),NavigableMap tailMap(K fromKey,boolean inclusive)和SortedMap subMap(K fromKey,K toKey)

Example

//your existing concurrent map changed to concurrent navigable map
NavigableMap throughputCounter = new ConcurrentSkipListMap();
            // this prints for inclusive values - 1 and 20 are both included
            System.out.println("How many calls(X number) came back in between 1 and 20 ms:" + calcThroughput(throughputCounter.subMap(1L, true, 20L, true)));
            System.out.println("How many calls(X number) came back in between 20 and 40 ms:" + calcThroughput(throughputCounter.subMap(20L, true, 40L, true)));
            System.out.println("How many calls(X number) came back in between 40 and 60 ms:" + calcThroughput(throughputCounter.subMap(40L, true, 60L, true)));
            System.out.println("How many calls(X number) came back in between 60 and 80 ms:" + calcThroughput(throughputCounter.subMap(60L, true, 80L, true)));
            System.out.println("How many calls(X number) came back in between 80 and 100 ms:" + calcThroughput(throughputCounter.subMap(80L, true, 100L, true)));
            System.out.println("How many calls(X number) came back in after 100 ms:" + calcThroughput(throughputCounter.tailMap(100L)));

    private Long calcThroughput(NavigableMap subMap) {
        Long sumOfARange = new Long(0);
        for (Long value : subMap.values()) {
            sumOfARange += value;
        }
        return sumOfARange;
    }

#5


0  

You don't need a map at all. You could simply divide the time by 20 (ms) and increment a counter in an array.

你根本不需要地图。您可以简单地将时间除以20(ms)并递增数组中的计数器。

public static void main( String[] args) {
    int counter[] = new int[6];
    for ( int i = 0 ; i <100 ; i++ ) {
        int time = (int) ( Math.random() * 200 );
        System.out.println( "" + time  );
        // add sample
        int idx = time / 20;
        idx = Math.min( idx, counter.length-1);
        counter[idx]++;
    }

    for ( int i = 0 ; i 

Please note that the last array element holds the number of all samples >= 100ms, thus the output should be corrected. Omitted to make the code as short and clear as possible.

请注意,最后一个数组元素保存所有样本的数量> = 100ms,因此应更正输出。省略尽可能简短明了的代码。

Example output

13 came back in between 0 and 20 ms
10 came back in between 20 and 40 ms
13 came back in between 40 and 60 ms
10 came back in between 60 and 80 ms
11 came back in between 80 and 100 ms
43 came back in between 100 and 120 ms

UPDATE: The output as it should be

更新:输出应该是

for ( int i = 0 ; i 

推荐阅读
  • HashMap的相关问题及其底层数据结构和操作流程
    本文介绍了关于HashMap的相关问题,包括其底层数据结构、JDK1.7和JDK1.8的差异、红黑树的使用、扩容和树化的条件、退化为链表的情况、索引的计算方法、hashcode和hash()方法的作用、数组容量的选择、Put方法的流程以及并发问题下的操作。文章还提到了扩容死链和数据错乱的问题,并探讨了key的设计要求。对于对Java面试中的HashMap问题感兴趣的读者,本文将为您提供一些有用的技术和经验。 ... [详细]
  • HashMap的扩容知识详解
    本文详细介绍了HashMap的扩容知识,包括扩容的概述、扩容条件以及1.7版本中的扩容方法。通过学习本文,读者可以全面了解HashMap的扩容机制,提升对HashMap的理解和应用能力。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了源码分析--ConcurrentHashMap与HashTable(JDK1.8)相关的知识,希望对你有一定的参考价值。  Concu ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了一个适用于PHP应用快速接入TRX和TRC20数字资产的开发包,该开发包支持使用自有Tron区块链节点的应用场景,也支持基于Tron官方公共API服务的轻量级部署场景。提供的功能包括生成地址、验证地址、查询余额、交易转账、查询最新区块和查询交易信息等。详细信息可参考tron-php的Github地址:https://github.com/Fenguoz/tron-php。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
author-avatar
魑魅魍魉龌蹉尴尬
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有