a possible low-level optimization
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的更多相关文章
- Solr实现Low Level查询解析(QParser)
Solr实现Low Level查询解析(QParser) Solr基于Lucene提供了方便的查询解析和搜索服务器的功能,可以以插件的方式集成,非常容易的扩展我们自己需要的查询解析方式.其中,Solr ...
- C++ Low level performance optimize 2
C++ Low level performance optimize 2 上一篇 文章讨论了一些底层代码的优化技巧,本文继续讨论一些相关的内容. 首先,上一篇文章讨论cache missing的重要性 ...
- C++ Low level performance optimize
C++ Low level performance optimize 1. May I have 1 bit ? 下面两段代码,哪一个占用空间更少,那个速度更快?思考10秒再继续往下看:) //v1 ...
- zabbix监控redis多实例(low level discovery)
对于多实例部署的tomcat.redis等应用,可以利用zabbix的low level discovery功能来实现监控,减少重复操作. 注:Zabbix版本: Zabbix 3.0.2 一.服务 ...
- 使用Java Low Level REST Client操作elasticsearch
Java REST客户端有两种风格: Java低级别REST客户端(Java Low Level REST Client,以后都简称低级客户端算了,难得码字):Elasticsearch的官方low- ...
- Zabbix监控Low level discovery实时监控网站URL状态
今天我们来聊一聊Low level discovery这个功能,我们为什么要用到loe level discovery这个功能呢? 很多时候,在使用zabbix监控一些东西,需要对类似于Itens进行 ...
- 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 ...
- Consumer设计-high/low Level Consumer
1 Producer和Consumer的数据推送拉取方式 Producer Producer通过主动Push的方式将消息发布到Broker n Consumer Consumer通过Pull从Br ...
- zabbix(10)自动发现规则(low level discovery)
1.概念 在配置Iterms的过程中,有时候需要对类似的Iterms进行添加,这些Iterms具有共同的特征,表现为某些特定的参数是变量,而其他设置都是一样的,例如:一个程序有多个端口,而需要对端口配 ...
- Elasticsearch java api操作(一)(Java Low Level Rest Client)
一.说明: 一.Elasticsearch提供了两个JAVA REST Client版本: 1.java low level rest client: 低级别的rest客户端,通过http与集群交互, ...
随机推荐
- Tensorflow教程
中文社区 tensorflow笔记:流程,概念和简单代码注释 TensorFlow入门教程集合 tensorboard教程:2017 TensorFlow 开发者峰会 TensorBoard轻松实践 ...
- OIer同样是音乐家
烦闷的时候,shenben为大家准备了2首歌(不用耳机也能听哦) 只需把代码复制到dev-c++的编辑器上,轻按F11,然后聆听OIer的音乐…… 千本樱 曲谱 #include <cstdio ...
- ifndef/define/endif 和 #ifdef 、#if 作用和用法
为了能简单的看看某些linux内核源码,复习了一下c语音,今天汇总了一下关于宏定义的相关内容: 一.ifndef/define/endif用法: .h文件,如下: #ifndef XX_H #defi ...
- 【BZOJ3679】数字之积 DFS+DP
[BZOJ3679]数字之积 Description 一个数x各个数位上的数之积记为f(x) <不含前导零>求[L,R)中满足0<f(x)<=n的数的个数 Input 第一行一 ...
- 远程访问(post 传参数) 以及IOUtils复制文件
package com.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream ...
- html学习笔记(2)-字母大小写转换练习
主要应用了text-transform属性值: uppercase:所有单词的字母都大写: lowercase:所有单词的字母都小写: capitalize:每个单词的首字母都大写: none:默认值 ...
- django mysql setting 设置
django mysql setting django mysql 设置 DATABASES = { 'default': { 'ENGINE': 'django.db.back ...
- Android学习之——优化篇(2)
一.高级优化 上篇主要从0基础优化的方式,本篇主要将从程序执行性能的角度出发,分析各种经常使用方案的不足.并给出对象池技术.基础数据类型替换法.屏蔽函数计算三种能够节省资源开销和处理器时间的优 ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- requests不加代理
requests里的proxies不加代理可以设置为空,就会使用本机IP proxies={}