问题

-对多维度特征进行topk排序,使用c++ 优先队列模拟最大堆.


/*
----------------------------------
Version : ??
File Name : dm2.py
Description :
Author : xijun1
Email :
Date : 2018/12/7
-----------------------------------
Change Activity : 2018/12/7
-----------------------------------
__author__ = 'xijun1'
*/
#include <iostream>
#include <queue>
#include <unordered_map>
#include <future>
using namespace std; class MinHeapUtil {
private: struct HeapGreatcompare
{
// min heap
bool operator()(const pair<unsigned int, int>& node_a, const pair<unsigned int, int>& node_b) {
return node_a.second > node_b.second;
}
}; std::vector< std::priority_queue< std::pair<unsigned int, int>, std::vector< std::pair<unsigned int, int> > ,HeapGreatcompare> > pq_mat;
unsigned int max_topk;
public:
bool setShape(const int dim , const unsigned int max_topk );
int getdim(){
return this->pq_mat.size();
}
int getMaxTopK(){
return this->max_topk;
}
size_t getSize( int k ){
return this->pq_mat[ k ].size();
}
int push_back(std::vector<std::pair<unsigned int, int> >& data , int k );
int push_back( std::pair<unsigned int, int> word_prob , int k );
int push_back( std::pair<unsigned int, int> &word_prob , int k );
std::pair<unsigned int , int >get_itop(int k){
auto word_prob = this->pq_mat[ k ].top();
this->pq_mat[ k ].pop();
return word_prob;
}
}; bool MinHeapUtil::setShape(const int dim, const unsigned int max_topk) {
this->pq_mat.resize(dim );
this->max_topk = max_topk;
return true;
}
int MinHeapUtil::push_back(std::vector< std::pair<unsigned int, int> > &data ,int k) { for (auto const& word_prob : data) {
this->pq_mat[ k ].push(std::make_pair(word_prob.second, word_prob.first));
if ( this->pq_mat[ k ].size() > this->max_topk) //维护这个K大小
this->pq_mat[ k ].pop();
}
return 0;
}
int MinHeapUtil::push_back(std::pair<unsigned int, int> word_prob, int k) { if( this->pq_mat[k].size() < this->max_topk || this->pq_mat[ k ].top().second < word_prob.second) {
this->pq_mat[k].push(word_prob);
if (this->pq_mat[k].size() > this->max_topk) //维护这个K大小
this->pq_mat[k].pop();
}
return 0; } int MinHeapUtil::push_back(std::pair<unsigned int, int> &word_prob, int k) {
this->pq_mat[k].push(word_prob);
if (this->pq_mat[k].size() > this->max_topk) //维护这个K大小
this->pq_mat[k].pop();
return 0;
}
std::unordered_map<unsigned int , std::vector< int > > test_data;
int main() {
MinHeapUtil top_words ; top_words.setShape(5,4);
int total_size = 5; // MinHeapUtil.push_back(std::pair<unsigned int, int>(8,2),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(7,3),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(6,4),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(5,5),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(4,6),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(3,7),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(2,4),0);
// MinHeapUtil.push_back(std::pair<unsigned int, int>(1,8),0);
for (int j = 0; j < 1000000; ++j) {
std::vector< int > tmp(5);
for (int i = 0; i < 5; ++i) {
tmp[i] = j*5 + i;
}
test_data[j] = std::move(tmp);
}
struct save_func
{
void operator () (int tn ,int thread_nums,int total_size , MinHeapUtil *top_words )
{
std::cout<<tn <<" : "<< thread_nums<<" : "<<total_size<<std::endl;
for (int i = tn; i < total_size ; i+=thread_nums) {
for ( auto word_key : test_data) {
//std::cout<<word_key.first;
top_words->push_back( std::pair<unsigned int, int>(word_key.first, word_key.second[ i ] ) , i );
}
}
std::cout << "func " << tn << std::endl;
}
} func;
unsigned thread_nums = 8;
std::vector<std::future< void >> muliti_topword_threads; for (unsigned tn = 0; tn < thread_nums; ++tn)
muliti_topword_threads.emplace_back(
std::async(std::launch::async, func, (int)tn,(int)thread_nums,total_size,&top_words)); for (unsigned tn = 0; tn < thread_nums; ++tn)
muliti_topword_threads[tn].get(); for (int k = 0; k < total_size; ++k) {
size_t range = top_words.getSize( k );
for (int i = 0; i < range; ++i) {
auto temp = top_words.get_itop( k );
std::cout <<"k:"<<k<<" ( " << temp.first << " " << temp.second << ")\n";
}
}
return 0;
}

--结果

01 :  : 88 :  : 55

2 : 8 : 5
3 : 8 : 5
4 : 8 : 5
5 : 8 : 5
func 5
6 : 8 : 5
func 6
7 : 8 : 5
func 7
func 1
func 4
func 0
func 2
func 3
k:0 ( 999996 4999980)
k:0 ( 999997 4999985)
k:0 ( 999998 4999990)
k:0 ( 999999 4999995)
k:1 ( 999996 4999981)
k:1 ( 999997 4999986)
k:1 ( 999998 4999991)
k:1 ( 999999 4999996)
k:2 ( 999996 4999982)
k:2 ( 999997 4999987)
k:2 ( 999998 4999992)
k:2 ( 999999 4999997)
k:3 ( 999996 4999983)
k:3 ( 999997 4999988)
k:3 ( 999998 4999993)
k:3 ( 999999 4999998)
k:4 ( 999996 4999984)
k:4 ( 999997 4999989)
k:4 ( 999998 4999994)
k:4 ( 999999 4999999)

多线程处理N维度topk问题demo--[c++]的更多相关文章

  1. 数据可视化--> numpy

    一.NumPy 1.简介: 官网链接:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库 ...

  2. Python: Socket网络编程,多线程处理小Demo

    一个简单的例子,深入研究一下socket的多线程处理任务 Server端: #!/usr/bin/env python #encoding:utf8 # # 注意:定义encoding时必须在第二行 ...

  3. Topk引发的一些简单的思考

    软件工程课程的一个题目:写一个程序,分析一个文本文件中各个词出现的频率,并且把频率最高的10个词打印出来.文本文件大约是30KB~300KB大小. 首先说一下这边的具体的实现都是在linux上实现的. ...

  4. Java Design Demo -简单的队列-异步多任务队列(java android)

    简单的单线程队列 -- 工作的时候遇到劣质打印机.给打印机发消息,打印机就会打印,如果在打印机还在打印的时候,就 再发消息打印,就会出现消息丢失.所以需要给上一个任务一些处理的间隔时间. 单线程的消息 ...

  5. Android和C#实时视频传输Demo

    说起去年的Demo.以今天的免费整齐优势. 原理很easy,虽然没有写android申请书.但,好了~ 高级语言是相通的.傲慢约.就这么简单研究了一下api后,找到相机对象有一个预览回调方法. 意识到 ...

  6. 关于echarts的一些基本使用demo

    最近发现一个很好用的一个前端控件echarts,效果非常不错,兼容ie8+以上等主流浏览器.可以使用它制作报表,地图示意图等,可用其实现一系列强大的功能. 其基于html5 Canvas,是一个纯Ja ...

  7. [小北De编程手记] Lesson 01 - AutoFramework构建 之 从一个简单的Demo聊起

    写在最前面 这个系列的主旨是要跟大家分享一下关于自动化测试框架的构建的一些心得.这几年,做了一些自动化测试框架以及团队的构建的工作.过程中遇到了很多这样的同学,他们在学习了某一门语言和一些自动化测试的 ...

  8. 集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序

    在2015年8月份的时候,决心学习图像算法. 几乎把当时市面上的图像算法相关书籍都看了一遍, 资金有限,采取淘宝买二手书,长期驻留深圳图书馆的做法, 进度总是很慢,学习算法不得其法. 虽然把手上所有书 ...

  9. 基于BaseAdapter的Listview小Demo

    ListView是android开发中比较常用的控件, 其中适配器模式可以选择: ArrayAdapter:简单易用,通常用于将数组或者List集合的读个包值封装成多个列表项 SimpleAdapte ...

随机推荐

  1. (openssl_pkey_get_private 函数不存在)phpstudy开启openssl.dll 时提示httpd.exe 丢失libssl-1_1.dll

    下载libssl-1_1.dll  丢到apache目录下的bin目录下(貌似要32位的)

  2. Vue 中 computed、watch对比

    computed:就像调用VUE的DATA一样 watch的对比 :监听事件

  3. M × N Puzzle POJ - 2893(奇数码)

    The Eight Puzzle, among other sliding-tile puzzles, is one of the famous problems in artificial inte ...

  4. SpringBoot的国际化使用

    在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...

  5. 【ACM】 1231 最大连续子序列

    [1231 最大连续子序列 ** Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  6. Android的系统属性:build.propSystemProperties

    获取build.prop的键值信息: String sn = SystemProperties.get(SN_INFO); 其中key值为: public static final String SN ...

  7. 【开源GPS追踪】 之 硬件开源

    根据设定目标: 使用GPS 采集经纬度,然后通过GPRS模块/wifi 发送到服务器显示,WIFI不常有,所有就使用GPRS模块! 对于GPS模块,没有特殊要求,只要输出格式符合NMEA协议即可,为了 ...

  8. 面试题fugui02

    一.概念题 1.描述对super.pass.yield.lambda关键字修饰的理解 2.大致描述一下python GIL的机制,以及python中多线程和多进程的区别 GIL全局解释器锁,是pyth ...

  9. shell脚本使用技巧3--函数调用

    定义函数 function fname() { statements; } 或者 fname() { statements; } 传递参数给函数: fname arg1 arg2; ex: 函数参数定 ...

  10. Maven创建项目

    Maven创建项目 Maven翻译成中文是『专家.内行』.Maven是Apache组织中一个颇为成功的开源项目,Maven主要服务于基于Java平台的项目构建.依赖管理和项目信息管理的优秀工具 本文将 ...