并发MD5计算方法
MD5与SHA算法一样,利用他们可以计算某段数据的唯一hash值,常用做校验码。而MD5比SHA算法性能高。在我参加的一个项目中,主要用MD5码值来去重,因此对计算性能要求较高。网上有对MD5算法并行化方法,能保证计算结果与线性计算法一样。由于我只需要唯一标记,并不要求它与线性MD5计算结果一样,所以,我通过分片多线程计算的方法实现提速,也充分利用了多核CPU的并行计算优势。
依据:
B = (b1,b2,b3,b4...bn)--->(m1,m2,m3...mn)----+--->M---->(md5) result = 16 bytes
C#代码:
class SafeQueue<T>
{
// A queue that is protected by Monitor.
private Queue<T> m_inputQueue = new Queue<T>(); // Lock the queue and add an element.
public void Enqueue(T qValue)
{
// Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, add an element.
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
} // Try to add an element to the queue: Add the element to the queue
// only if the lock is immediately available.
public bool TryEnqueue(T qValue)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
} // Try to add an element to the queue: Add the element to the queue
// only if the lock becomes available during the specified time
// interval.
public bool TryEnqueue(T qValue, int waitTime)
{
// Request the lock.
if (Monitor.TryEnter(m_inputQueue, waitTime))
{
try
{
m_inputQueue.Enqueue(qValue);
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
}
return true;
}
else
{
return false;
}
} // Lock the queue and dequeue an element.
public T Dequeue()
{
T retval; // Request the lock, and block until it is obtained.
Monitor.Enter(m_inputQueue);
try
{
// When the lock is obtained, dequeue an element.
retval = m_inputQueue.Dequeue();
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return retval;
} // Delete all elements that equal the given object.
public int Remove(T qValue)
{
int removedCt = ; // Wait until the lock is available and lock the queue.
Monitor.Enter(m_inputQueue);
try
{
int counter = m_inputQueue.Count;
while (counter > )
// Check each element.
{
T elem = m_inputQueue.Dequeue();
if (!elem.Equals(qValue))
{
m_inputQueue.Enqueue(elem);
}
else
{
// Keep a count of items removed.
removedCt += ;
}
counter = counter - ;
}
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return removedCt;
} // Print all queue elements.
public string PrintAllElements()
{
StringBuilder output = new StringBuilder(); // Lock the queue.
Monitor.Enter(m_inputQueue);
try
{
foreach( T elem in m_inputQueue )
{
// Print the next element.
output.AppendLine(elem.ToString());
}
}
finally
{
// Ensure that the lock is released.
Monitor.Exit(m_inputQueue);
} return output.ToString();
}
} //任务节点
public class TaskItem
{
public int idx_;
public byte[] data_;
public TaskItem( int idx, byte[] data )
{
this.idx_ = idx;
this.data_ = data;
}
}
//工作线程
public class WorkThread
{
SafeQueue<TaskItem> packet_;
AutoResetEvent notifyEvt_;
ManualResetEvent finishEvt_;
Thread thread_;
HashTable result_; public WorkThread()
{
....
thread_ = new Thread( new ThreadStart( this.DoWork ) );
}
public void Start()
{
thread_.Start();
}
//主线程调用:异步添加任务节点
public void AddDataPacket( TaskItem data )
{
packet_.Enqueue( data );
notifyEvt_.Set(); }
//主线程调用:等待收集计算结果
public Hashtable Finish()
{
Hashtable rs = new Hashtable( result_ );
finishEvt_.WaitOne();
finishEvt_.Reset();
result_.clear();
return rs;
}
//线程执行函数
public void DoWork()
{
TaskItem var;
while(true)
{
notifyEvt_.WaitOne();
while(true)
{
if( packet_.Count > )
{
//这里取出值错误
var = packet_.Dequeue();
}
if( var == null )
break;
//task finish
if( var.Idx == - )
{
....
this.finishEvt_.Set();
}
}
}
}
} public class Md5Hasher
{
public void main()
{
WorkThread[] test=new WorkThread[]; for( int i=; i<; ++i )
{
//这里输入数据
test[i%].AddDataPacket( ... );
}
}
}
并发MD5计算方法的更多相关文章
- 阿里P8面试官:如何设计一个扛住千万级并发的架构?
大家先思考一个问题,这也是在面试过程中经常遇到的问题. 如果你们公司现在的产品能够支持10W用户访问,你们老板突然和你说,融到钱了,会大量投放广告,预计在1个月后用户量会达到1000W,如果这个任务交 ...
- 演示get、post请求如何算sn,算得sn如何使用
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...
- python查找并删除相同文件-UNIQ File-wxPython-v6
相比第一版,新增:菜单,对话框,文件过滤器,操作结果保存,配置功能(自己写了一个读写配置文件的功能),提示语优化,模块分化更合理. 截图: 源代码: UniqFile-wxPython-v6.py: ...
- python查找并删除相同文件-UNIQ File-script版本
今天用wxPython做了一个GUI程序,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有文件都计算,大大减少了md5 ...
- python查找并删除相同文件-UNIQ File-wxPython版本
今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...
- 百度GPSutil
================================================= package com.qcar.benz.biz.common; import com.aliba ...
- HTTP服务简介
第1章 HTTP服务介绍 1.1 简述用户访网站流程 a 进行域名信息的DNS解析 dig +trace 获得www.oldboyedu.com ip地址信息 b 进行与网站服务器建立连接,tc ...
- 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件
目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...
- vue-simple-uploader上传插件
基于vue-simple-uploader封装文件分片上传.秒传及断点续传的全局上传插件 https://www.cnblogs.com/xiahj/p/vue-simple-uploader.htm ...
随机推荐
- javascript !!作用
javaScript中使用!!表示取得boolean值,具体作用如下 var value= !!test[1]; 取变量的Boolean值, 相当于 var value = test[1]?true: ...
- JavaFX 2 Dialogs
http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/ ———————————————————————————————————————————— ...
- 【转】iOS开发工具系列(按功能分)
http://www.cocoachina.com/newbie/basic/2014/0417/8187.html 这是我们多篇iOS开发工具系列篇中的一篇,此前的文章比如:那些不能错过的Xcode ...
- DBMS_ERRLOG记录DML错误日志(二)
上一篇简单介绍了DML记录语句的限制,虽然所有的例子都是利用INSERT语句,但是LOG ERRORS语句并没有这个限制,UPDATE.DELETE和MERGE都可以使用这个语句.下面要说的就是这篇的 ...
- Gym 100507C Zhenya moves from parents (线段树)
Zhenya moves from parents 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/C Description Z ...
- (转)UML实践----用例图、类图、对象图、顺序图、协作图、状态图、活动图、组件图、配置图
面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language™),这篇课程的目的是展示出UML ...
- 使用https时,网站一些内容不能正常显示的问题
在网站开发过程中,使用http网站页面一切正常. 但改成https后,发现网站一些页面不能正常显示出来,比如看上去没有样式等. 原因是: 在程序中调用了比如JQuery,而引用的URL使用的是Http ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- Spring MVC Framework 注解
ControllerAdvice Spring MVC Framework会把 @ControllerAdvice注解内部使用 @ExceptionHandler.@InitBinder.@Model ...
- WinDbug之DUMP蓝屏分析
Microsoft (R) Windows Debugger Version 6.2.8400.0 X86Copyright (c) Microsoft Corporation. All rights ...