📄 bench_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Crypto++: bench.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.2 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul></div><div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul></div><h1>bench.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// bench.cpp - written and placed in the public domain by Wei Dai</span><a name="l00002"></a>00002 <a name="l00003"></a>00003 <span class="preprocessor">#define _CRT_SECURE_NO_DEPRECATE</span><a name="l00004"></a>00004 <span class="preprocessor"></span><a name="l00005"></a>00005 <span class="preprocessor">#include "bench.h"</span><a name="l00006"></a>00006 <span class="preprocessor">#include "crc.h"</span><a name="l00007"></a>00007 <span class="preprocessor">#include "adler32.h"</span><a name="l00008"></a>00008 <span class="preprocessor">#include "<a class="code" href="idea_8h.html">idea.h</a>"</span><a name="l00009"></a>00009 <span class="preprocessor">#include "<a class="code" href="des_8h.html">des.h</a>"</span><a name="l00010"></a>00010 <span class="preprocessor">#include "<a class="code" href="rc5_8h.html">rc5.h</a>"</span><a name="l00011"></a>00011 <span class="preprocessor">#include "<a class="code" href="blowfish_8h.html">blowfish.h</a>"</span><a name="l00012"></a>00012 <span class="preprocessor">#include "wake.h"</span><a name="l00013"></a>00013 <span class="preprocessor">#include "<a class="code" href="cast_8h.html">cast.h</a>"</span><a name="l00014"></a>00014 <span class="preprocessor">#include "seal.h"</span><a name="l00015"></a>00015 <span class="preprocessor">#include "<a class="code" href="rc6_8h.html">rc6.h</a>"</span><a name="l00016"></a>00016 <span class="preprocessor">#include "<a class="code" href="mars_8h.html">mars.h</a>"</span><a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="twofish_8h.html">twofish.h</a>"</span><a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="serpent_8h.html">serpent.h</a>"</span><a name="l00019"></a>00019 <span class="preprocessor">#include "<a class="code" href="skipjack_8h.html">skipjack.h</a>"</span><a name="l00020"></a>00020 <span class="preprocessor">#include "cbcmac.h"</span><a name="l00021"></a>00021 <span class="preprocessor">#include "dmac.h"</span><a name="l00022"></a>00022 <span class="preprocessor">#include "aes.h"</span><a name="l00023"></a>00023 <span class="preprocessor">#include "blumshub.h"</span><a name="l00024"></a>00024 <span class="preprocessor">#include "rng.h"</span><a name="l00025"></a>00025 <span class="preprocessor">#include "files.h"</span><a name="l00026"></a>00026 <span class="preprocessor">#include "hex.h"</span><a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="modes_8h.html">modes.h</a>"</span><a name="l00028"></a>00028 <span class="preprocessor">#include "factory.h"</span><a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="preprocessor">#include <time.h></span><a name="l00031"></a>00031 <span class="preprocessor">#include <math.h></span><a name="l00032"></a>00032 <span class="preprocessor">#include <iostream></span><a name="l00033"></a>00033 <span class="preprocessor">#include <iomanip></span><a name="l00034"></a>00034 <a name="l00035"></a>00035 USING_NAMESPACE(CryptoPP)<a name="l00036"></a>00036 USING_NAMESPACE(std)<a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="preprocessor">#ifdef CLOCKS_PER_SEC</span><a name="l00039"></a>00039 <span class="preprocessor"></span><span class="keyword">const</span> <span class="keywordtype">double</span> CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;<a name="l00040"></a>00040 <span class="preprocessor">#elif defined(CLK_TCK)</span><a name="l00041"></a>00041 <span class="preprocessor"></span><span class="keyword">const</span> <span class="keywordtype">double</span> CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;<a name="l00042"></a>00042 <span class="preprocessor">#else</span><a name="l00043"></a>00043 <span class="preprocessor"></span><span class="keyword">const</span> <span class="keywordtype">double</span> CLOCK_TICKS_PER_SECOND = 1000000.0;<a name="l00044"></a>00044 <span class="preprocessor">#endif</span><a name="l00045"></a>00045 <span class="preprocessor"></span><a name="l00046"></a>00046 <span class="keywordtype">double</span> logtotal = 0, g_allocatedTime, g_hertz;<a name="l00047"></a>00047 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> logcount = 0;<a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="keyword">static</span> <span class="keyword">const</span> byte *<span class="keyword">const</span> key=(byte *)<span class="stringliteral">"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"</span>;<a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="keywordtype">void</span> OutputResultBytes(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keywordtype">double</span> length, <span class="keywordtype">double</span> timeTaken)<a name="l00052"></a>00052 {<a name="l00053"></a>00053 <span class="keywordtype">double</span> mbs = length / timeTaken / (1024*1024);<a name="l00054"></a>00054 cout << <span class="stringliteral">"\n<TR><TH>"</span> << name;<a name="l00055"></a>00055 <span class="comment">// cout << "<TD>" << setprecision(3) << length / (1024*1024);</span><a name="l00056"></a>00056 cout << setiosflags(ios::fixed);<a name="l00057"></a>00057 <span class="comment">// cout << "<TD>" << setprecision(3) << timeTaken;</span><a name="l00058"></a>00058 cout << <span class="stringliteral">"<TD>"</span> << setprecision(0) << setiosflags(ios::fixed) << mbs;<a name="l00059"></a>00059 <span class="keywordflow">if</span> (g_hertz)<a name="l00060"></a>00060 cout << <span class="stringliteral">"<TD>"</span> << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length;<a name="l00061"></a>00061 cout << resetiosflags(ios::fixed);<a name="l00062"></a>00062 logtotal += log(mbs);<a name="l00063"></a>00063 logcount++;<a name="l00064"></a>00064 }<a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keywordtype">void</span> OutputResultKeying(<span class="keywordtype">double</span> iterations, <span class="keywordtype">double</span> timeTaken)<a name="l00067"></a>00067 {<a name="l00068"></a>00068 cout << <span class="stringliteral">"<TD>"</span> << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations);<a name="l00069"></a>00069 <span class="keywordflow">if</span> (g_hertz)<a name="l00070"></a>00070 cout << <span class="stringliteral">"<TD>"</span> << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations;<a name="l00071"></a>00071 }<a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="keywordtype">void</span> OutputResultOperations(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <span class="keyword">const</span> <span class="keywordtype">char</span> *operation, <span class="keywordtype">bool</span> pc, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> iterations, <span class="keywordtype">double</span> timeTaken)<a name="l00074"></a>00074 {<a name="l00075"></a>00075 cout << <span class="stringliteral">"\n<TR><TH>"</span> << name << <span class="stringliteral">" "</span> << operation << (pc ? <span class="stringliteral">" with precomputation"</span> : <span class="stringliteral">""</span>);<a name="l00076"></a>00076 <span class="comment">// cout << "<TD>" << iterations;</span><a name="l00077"></a>00077 <span class="comment">// cout << setiosflags(ios::fixed);</span><a name="l00078"></a>00078 <span class="comment">// cout << "<TD>" << setprecision(3) << timeTaken;</span><a name="l00079"></a>00079 cout << <span class="stringliteral">"<TD>"</span> << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations);<a name="l00080"></a>00080 <span class="keywordflow">if</span> (g_hertz)<a name="l00081"></a>00081 cout << <span class="stringliteral">"<TD>"</span> << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000;<a name="l00082"></a>00082 cout << resetiosflags(ios::fixed);<a name="l00083"></a>00083 <a name="l00084"></a>00084 logtotal += log(iterations/timeTaken);<a name="l00085"></a>00085 logcount++;<a name="l00086"></a>00086 }<a name="l00087"></a>00087 <a name="l00088"></a>00088 <span class="keywordtype">void</span> BenchMark(<span class="keyword">const</span> <span class="keywordtype">char</span> *name, <a class="code" href="class_block_transformation.html" title="interface for the data processing part of block ciphers">BlockTransformation</a> &cipher, <span class="keywordtype">double</span> timeTotal)<a name="l00089"></a>00089 {<a name="l00090"></a>00090 <span class="keyword">const</span> <span class="keywordtype">int</span> BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.<a class="code" href="class_block_transformation.html#9ee1da34a4c957d4be87d2b0469806b0" title="return number of blocks that can be processed in parallel, for bit-slicing implementations...">OptimalNumberOfParallelBlocks</a>() * cipher.<a class="code" href="class_block_transformation.html#4936bef4368804cf5cadab9eb6eb37e4" title="block size of the cipher in bytes">BlockSize</a>());<a name="l00091"></a>00091 <a class="code" href="class_sec_block.html" title="a block of memory allocated using A">AlignedSecByteBlock</a> buf(BUF_SIZE);<a name="l00092"></a>00092 <span class="keyword">const</span> <span class="keywordtype">int</span> nBlocks = BUF_SIZE / cipher.<a class="code" href="class_block_transformation.html#4936bef4368804cf5cadab9eb6eb37e4" title="block size of the cipher in bytes">BlockSize</a>();<a name="l00093"></a>00093 clock_t start = clock();<a name="l00094"></a>00094 <a name="l00095"></a>00095 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> i=0, blocks=1;<a name="l00096"></a>00096 <span class="keywordtype">double</span> timeTaken;<a name="l00097"></a>00097 <span class="keywordflow">do</span><a name="l00098"></a>00098 {<a name="l00099"></a>00099 blocks *= 2;<a name="l00100"></a>00100 <span class="keywordflow">for</span> (; i<blocks; i++)<a name="l00101"></a>00101 cipher.<a class="code" href="class_block_transformation.html#9efa0ea24795a19d15c9aa0d7f134562" title="encrypt or decrypt multiple blocks, for bit-slicing implementations">ProcessAndXorMultipleBlocks</a>(buf, NULL, buf, nBlocks);<a name="l00102"></a>00102 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;<a name="l00103"></a>00103 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -