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计算方法的更多相关文章

  1. 阿里P8面试官:如何设计一个扛住千万级并发的架构?

    大家先思考一个问题,这也是在面试过程中经常遇到的问题. 如果你们公司现在的产品能够支持10W用户访问,你们老板突然和你说,融到钱了,会大量投放广告,预计在1个月后用户量会达到1000W,如果这个任务交 ...

  2. 演示get、post请求如何算sn,算得sn如何使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncoding ...

  3. python查找并删除相同文件-UNIQ File-wxPython-v6

    相比第一版,新增:菜单,对话框,文件过滤器,操作结果保存,配置功能(自己写了一个读写配置文件的功能),提示语优化,模块分化更合理. 截图: 源代码: UniqFile-wxPython-v6.py: ...

  4. python查找并删除相同文件-UNIQ File-script版本

    今天用wxPython做了一个GUI程序,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有文件都计算,大大减少了md5 ...

  5. python查找并删除相同文件-UNIQ File-wxPython版本

    今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...

  6. 百度GPSutil

    ================================================= package com.qcar.benz.biz.common; import com.aliba ...

  7. HTTP服务简介

    第1章 HTTP服务介绍 1.1 简述用户访网站流程 a 进行域名信息的DNS解析   dig +trace 获得www.oldboyedu.com  ip地址信息 b 进行与网站服务器建立连接,tc ...

  8. 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件

    目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...

  9. vue-simple-uploader上传插件

    基于vue-simple-uploader封装文件分片上传.秒传及断点续传的全局上传插件 https://www.cnblogs.com/xiahj/p/vue-simple-uploader.htm ...

随机推荐

  1. 【恒天云技术分享系列11】Sheepdog简介

    sheepdog是近几年开源社区新兴的分布式块存储文件系统,采用完全对称的结构,没有类似元数据服务的中心节点.这种架构带来了线性可扩展性,没有单点故障和容易管理的特性.对于磁盘和物理节点,SheepD ...

  2. 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程

    一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...

  3. 【Hadoop代码笔记】Hadoop作业提交之Child启动map任务

    一.概要描述 在上篇博文描述了TaskTracker启动一个独立的java进程来执行Map或Reduce任务.在本篇和下篇博文中我们会关注启动的那个入口是org.apache.hadoop.mapre ...

  4. 使用SignalR实现比特币价格实时刷新

    ASP.NET SignalR是微软支持的一个运行在 Dot NET 平台上的 HTML Websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重 ...

  5. js滑动门及对像的使用

    function scrollDoor() { } scrollDoor.prototype = { sd: function (menus, divs, openClass, closeClass) ...

  6. Spark生态之Spark Core

  7. 【现代程序设计】【homework-08】

    1. 理解C++变量的作用域和生命周期 #include<stdio.h> char * test() { ]="; return s; } main() { puts(test ...

  8. 精通Linux的“kill”命令

    无论你使用哪种操作系统,你一定会遇到某个行为失常的应用,它把自己锁死并拒绝关闭.在Linux(还有Mac),你可以用一个"kill"命令强制终结它.在这个教程中,我们将展示给你多种 ...

  9. VS2012与NUnit

    微软提供的NUnit插件是针对vs2010的,而vs2012会自动识别,测试环境为64位win7,具体操作步骤如下 1.下载安装NUnit(NUnit-2.6.3.msi) 2.新建测试项目UnitT ...

  10. Hadoop本地库

    目的 鉴于性能问题以及某些Java类库的缺失,对于某些组件,Hadoop提供了自己的本地实现. 这些组件保存在Hadoop的一个独立的动态链接的库里.这个库在*nix平台上叫libhadoop.so. ...