问题

-对多维度特征进行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. POJ 1094 Sorting It All Out 【拓扑排序】

    <题目链接> 题目大意: 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序. 解题分析 ...

  2. RESTful架构&简单使用Django rest framework

    RESTful架构 1 什么是REST REST全称是Representational State Transfer,中文意思是表述性状态转移. 它首次出现在2000年Roy Fielding的博士论 ...

  3. Django分页(一)

    Django分页(一) 手动实现简单分页 HTML <!DOCTYPE html> <html lang="en"> <head> <me ...

  4. hdu3944

    hdu3944题目中给出的杨辉三角形的形状带有误导目的,应该转化成对称的形状再去思考这个问题分两种情况第一个是在左区从目标位置向左上方走一直走到边界,然后再向右上方一直走到起点n-k个1加上C(n-k ...

  5. js算法初窥07(算法复杂度)

    算法复杂度是我们来衡量一个算法执行效率的一个度量标准,算法复杂度通常主要有时间复杂度和空间复杂度两种.时间复杂度就是指算法代码在运行最终得到我们想要的结果时所消耗的时间,而空间复杂度则是指算法中用来存 ...

  6. MySQL数据库基本用法-聚合-分组

    聚合 为了快速得到统计数据,提供了5个聚合函数 count(*)表示计算总行数,括号中写星与列名,结果是相同的 查询学生总数 select count(*) from students; max(列) ...

  7. scrapy 安装

    windows 1.wheelpip install wheel2.lxmlhttp://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml3.PyOpensslhttp ...

  8. angular笔记_1

    第一个angular文件<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js&q ...

  9. 安装淘宝镜像cnpm时出现问题及解决方案

    问题: 解决方案: 安装完成:

  10. CC2431 代码分析③-忍辱负重的CC2430

    这节主要分析CC2430的代码,是参考节点的代码,协调器代码我们放到最后分析. 代码分析的原则是事件为导向,我们从CC2431 盲节点code的分析中发现CC2431 向CC2430参考节点发送的信息 ...