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. Unity3D自定义编辑器简单实例

    MenuItem:在标题栏自定义菜单.需要在Editor文件夹内创建脚本,无需挂载.但是注意其下的函数必须为静态函数. using UnityEngine; using UnityEditor; pu ...

  2. Kindeditor 修改内容时如何不让&nbsp;及 <> 被自动转义

    $html = str_replace(' ', '&nbsp;', $html); $html = str_replace('>', '&gt;', $html); $html ...

  3. 【BZOJ1316】树上的询问 点分治+set

    [BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...

  4. mysql date函数相关用法整理(持续更新)

    date_add(now(), INTERVAL 1 day)   增加一天 date_format(d,'%Y-%m-%d %T')   这里的d为datestamp类型,格式化成  yyyy-MM ...

  5. Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.

    $ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...

  6. python get post模拟请求

    1.使用get方式时.url相似例如以下格式: &op=bind   GET报问头例如以下: &n=asa HTTP/1.1    Accept: */*    Accept-Lang ...

  7. COPY SAP 标准gui状态

    [转]如何COPY SAP标准gui状态 1.可以自己建立 2.找到合适的ALV程序,然后找到合适的 gui_statu,进行copy. 但是这个是系统有过自定义开发会方便很多,如果没有,那要找标准程 ...

  8. ABAP excel操作 OLE 常用方法和属性

    转自 http://bstone.blog.163.com/blog/static/176820446201172834149199/#userconsent# OLE 常用方法和属性 1.ole中如 ...

  9. rocketmq安装

    1 安装Rocketmq https://my.oschina.net/daijunjian/blog/732560 2 可视化管理控台 http://blog.csdn.net/jayjjb/art ...

  10. SDUT OJ 螺旋矩阵

    螺旋方阵 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 n×n的螺旋方阵当n=5和n=3时分别是如下的形式 请给出一个程序,对于 ...