http://www.1point3acres.com/bbs/thread-212960-1-1.html

第二轮白人小哥,一开始问了一道至今不懂的问题,好像是给一个vector<uint8_t> nums, 然后又给一个256位的vector<int> counts,遍历nums,然后counts[nums]++,问如何进行优化,提示说要用到CPU cache之类的东西(完全不知道)。小白哥见我懵逼,后来又给了一道3sum,迅速做出。

uint8_t input[];
uint32_t count[];
void count_it()
{
for (int i = ; i < sizeof(input) / sizeof(input[]); i++) {
++count[input[i]];
}
}

how to optimize? possible points to consider:

a) target "count" array size is 4B*256=1KB, which can fit into L1 cache, so no need to worry about that;

b) input array access is sequential, which is actually cache friendly;

c) update to "count" could have false sharing, but given it's all in L1 cache, that's fine;

d) optimization 1: the loop could be unrolled to reduce loop check;

e) optimization 2: input array could be pre-fetched (i.e. insert PREFETCH instructions beforehand);

    for (int i = ; i < sizeof(input) / sizeof(input[]);) {
// typical cache size is 64 bytes
__builtin_prefetch(&input[i+], , ); // prefetch for read, high locality
for (int j = ; j < ; j++) {
int k = i + j * ;
++count[input[k]];
++count[input[k+]];
++count[input[k+]];
++count[input[k+]];
++count[input[k+]];
++count[input[k+]];
++count[input[k+]];
++count[input[k+]];
}
i += ;
}

(see https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/Other-Builtins.html for __builtin_prefetch)

f) optimization 3: multi-threading, but need to use lock instruction when incrementing the count;

g) optimization 4: vector extension CPU instructions: "gather" instruction to load sparse locations (count[xxx]) to a zmmx register (512bit, 64byte i.e. 16 integers), then it can process 16 input uchar8_t in one go; then add a constant 512bit integer which adds 1 to each integer. corresponding "scatter" instruction will store back the updated count.

第二轮白人小哥,一开始问了一道至今不懂的问题,好像是给一个vector<uint8_t> nums, 然后又给一个256位的vector<int> counts,遍历nums,然后counts[nums]++,问如何进行优化,提示说要用到CPU cache之类的东西(完全不知道)。小白哥见我懵逼,后来又给了一道3sum,迅速做出。

a possible low-level optimization的更多相关文章

  1. Solr实现Low Level查询解析(QParser)

    Solr实现Low Level查询解析(QParser) Solr基于Lucene提供了方便的查询解析和搜索服务器的功能,可以以插件的方式集成,非常容易的扩展我们自己需要的查询解析方式.其中,Solr ...

  2. C++ Low level performance optimize 2

    C++ Low level performance optimize 2 上一篇 文章讨论了一些底层代码的优化技巧,本文继续讨论一些相关的内容. 首先,上一篇文章讨论cache missing的重要性 ...

  3. C++ Low level performance optimize

    C++ Low level performance optimize 1.  May I have 1 bit ? 下面两段代码,哪一个占用空间更少,那个速度更快?思考10秒再继续往下看:) //v1 ...

  4. zabbix监控redis多实例(low level discovery)

    对于多实例部署的tomcat.redis等应用,可以利用zabbix的low level discovery功能来实现监控,减少重复操作.  注:Zabbix版本: Zabbix 3.0.2 一.服务 ...

  5. 使用Java Low Level REST Client操作elasticsearch

    Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...

  6. Zabbix监控Low level discovery实时监控网站URL状态

    今天我们来聊一聊Low level discovery这个功能,我们为什么要用到loe level discovery这个功能呢? 很多时候,在使用zabbix监控一些东西,需要对类似于Itens进行 ...

  7. ChibiOS/RT 2.6.9 CAN Low Level Driver for STM32

    /* ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio Licensed under the Apache License, Version 2 ...

  8. Consumer设计-high/low Level Consumer

    1 Producer和Consumer的数据推送拉取方式   Producer Producer通过主动Push的方式将消息发布到Broker n Consumer Consumer通过Pull从Br ...

  9. zabbix(10)自动发现规则(low level discovery)

    1.概念 在配置Iterms的过程中,有时候需要对类似的Iterms进行添加,这些Iterms具有共同的特征,表现为某些特定的参数是变量,而其他设置都是一样的,例如:一个程序有多个端口,而需要对端口配 ...

  10. Elasticsearch java api操作(一)(Java Low Level Rest Client)

    一.说明: 一.Elasticsearch提供了两个JAVA REST Client版本: 1.java low level rest client: 低级别的rest客户端,通过http与集群交互, ...

随机推荐

  1. testVC.modalPresentationStyle = UIModalPresentationFormSheet; 更改 VC大小

    本文转载至 http://www.cocoachina.com/bbs/simple/?t31199.html TestViewController *testVC = [[TestViewContr ...

  2. 宇视摄像机RTSP地址格式规则

    rtsp://{用户名}:{密码}@{ip}:{port}/video1/2/3,分别对应主/辅/三码流: 比如: rtsp://admin:admin@192.168.8.8:554/video1, ...

  3. ES通过API调整设置

    1.查询es的设置信息 2.查询单个索引的设置 3.设置复制集为0

  4. Consumer Group Example

    面向kafka编程 Consumer Group Example https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Ex ...

  5. 80端口未被占用,apache无法启动,命令行运行httpd.exe提示文档内容有错

    Apache无法启动,端口被占用的可能性比较大,所以建议大家还是先换端口试试,这个网上说的比较多,具体可参见http://www.cnblogs.com/zdan68/p/3855636.html. ...

  6. LeetCode 017 4Sum

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  7. Ubuntu PPPoE拨号上网指定网卡

    Just follow these steps: Check that the ethernet cable is properly connected Open Terminal Run sudo ...

  8. 采集练习(十二) python 采集之 xbmc 酷狗电台插件

    前段时间买了个树莓派才知道有xbmc这么强大的影音软件(后来我逐渐在 电脑.手机和机顶盒上安装xbmc),在树莓派上安装xbmc后树莓派就成为了机顶盒,后面在hdpfans论坛发现了jackyspy  ...

  9. Raspberry Pi3 ~ 使用eclipse进行远程调试

    为了开发方便需要在电脑上对树莓派进行远程Debug. l  在eclipse中安装交叉编译(参照开发环境搭建)    arm-linux-gnueabihf-gcc l  树莓派中检查是否安装了gdb ...

  10. mysql 创建用户与授权

    权限管理 我们都知道,最高权限管理者是 root 用户 , 它拥有着最高的权限操作,包括 : select(查询) ,update(修改) , delete(删除,有事没事都不要用这个,反正也不能给这 ...