多线程处理N维度topk问题demo--[c++]
问题
-对多维度特征进行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++]的更多相关文章
- 数据可视化--> numpy
一.NumPy 1.简介: 官网链接:http://www.numpy.org/ NumPy是Python语言的一个扩充程序库.支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库 ...
- Python: Socket网络编程,多线程处理小Demo
一个简单的例子,深入研究一下socket的多线程处理任务 Server端: #!/usr/bin/env python #encoding:utf8 # # 注意:定义encoding时必须在第二行 ...
- Topk引发的一些简单的思考
软件工程课程的一个题目:写一个程序,分析一个文本文件中各个词出现的频率,并且把频率最高的10个词打印出来.文本文件大约是30KB~300KB大小. 首先说一下这边的具体的实现都是在linux上实现的. ...
- Java Design Demo -简单的队列-异步多任务队列(java android)
简单的单线程队列 -- 工作的时候遇到劣质打印机.给打印机发消息,打印机就会打印,如果在打印机还在打印的时候,就 再发消息打印,就会出现消息丢失.所以需要给上一个任务一些处理的间隔时间. 单线程的消息 ...
- Android和C#实时视频传输Demo
说起去年的Demo.以今天的免费整齐优势. 原理很easy,虽然没有写android申请书.但,好了~ 高级语言是相通的.傲慢约.就这么简单研究了一下api后,找到相机对象有一个预览回调方法. 意识到 ...
- 关于echarts的一些基本使用demo
最近发现一个很好用的一个前端控件echarts,效果非常不错,兼容ie8+以上等主流浏览器.可以使用它制作报表,地图示意图等,可用其实现一系列强大的功能. 其基于html5 Canvas,是一个纯Ja ...
- [小北De编程手记] Lesson 01 - AutoFramework构建 之 从一个简单的Demo聊起
写在最前面 这个系列的主旨是要跟大家分享一下关于自动化测试框架的构建的一些心得.这几年,做了一些自动化测试框架以及团队的构建的工作.过程中遇到了很多这样的同学,他们在学习了某一门语言和一些自动化测试的 ...
- 集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序
在2015年8月份的时候,决心学习图像算法. 几乎把当时市面上的图像算法相关书籍都看了一遍, 资金有限,采取淘宝买二手书,长期驻留深圳图书馆的做法, 进度总是很慢,学习算法不得其法. 虽然把手上所有书 ...
- 基于BaseAdapter的Listview小Demo
ListView是android开发中比较常用的控件, 其中适配器模式可以选择: ArrayAdapter:简单易用,通常用于将数组或者List集合的读个包值封装成多个列表项 SimpleAdapte ...
随机推荐
- JavaEE 之 Spring Data JPA(二)
1.JPQL a.定义:Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起·使用这种语言编写的查询是可移植的,可以被编译成 ...
- c#接口与抽象类的区别(一)
abstract 修饰符用于表示所修饰的类是不完整的,并且它只能用作基类.抽象类与非抽象类在以下方面是不同的: 抽象类不能直接实例化,并且对抽象类使用 new 运算符是编译时错误.虽然一些变量和值在编 ...
- Alpha(7/10)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- XamarinSQLite教程添加索引
XamarinSQLite教程添加索引 索引可以提升数据库表的查询速度.下面为已存在的表添加索引,操作步骤如下: (1)右击Students,选择Add index…(beta)命令,弹出Add In ...
- Android动画曲线库AndroidEasingFunctions
Android动画曲线库AndroidEasingFunctions AndroidEasingFunction是基于Easing Function(缓动函数)的Android动画曲线库.它提供了九大 ...
- Centos6.5部署Rsyslog+cron+rsync备份服务器
1.前言 rsync是一种快速且功能非常广泛的文件复制工具.它可以在本地复制,通过任何远程shell复制到/从另一个主机复制,也可以复制到/从远程rsync守护进程.它提供了大量的选项,可以控制其行为 ...
- tomcat环境变量详细配置步骤
这篇文章主要为大家详细介绍了tomcat环境变量配置步骤,包括JDK环境变量配置,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了tomcat环境变量的配置教程,供大家参考,具体内容如下 1.=== ...
- python系统编程(六)
threading注意点 1. 线程执行代码的封装 通过上一小节,能够看出,通过使用threading模块能完成多任务的程序开发,为了让每个线程的封装性更完美,所以使用threading模块时,往往会 ...
- windows下安装mysql遇到的问题
windows下安装mysql5.6.41步骤:http://www.cnblogs.com/sjy18039225956/p/9203052.html 系统错误2详见MySQL安装过程net sta ...