From def575a6c23a59237ec9cf944cbf8db0d4b5dc71 Mon Sep 17 00:00:00 2001 From: lloyd Date: Fri, 21 Nov 2008 16:49:46 +0000 Subject: Add an example of using the benchmark system to choose the fastest SHA-1 implementation and then setting it as the default. --- doc/examples/hash_quickly.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 doc/examples/hash_quickly.cpp (limited to 'doc') diff --git a/doc/examples/hash_quickly.cpp b/doc/examples/hash_quickly.cpp new file mode 100644 index 000000000..d8d991d9e --- /dev/null +++ b/doc/examples/hash_quickly.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +/* +Try to find the fastest SHA-1 implementation and use it to hash +files. In most programs this isn't worth the bother and +overhead. However with large amount of input, it is worth it. On tests +on a Core2 system with the SHA-1 SSE2 code enabled, over a few hundred +Mb or so the overhead paid for itself. + +Of course you could also just do this once and save it as an +application config, which is probably the smart thing to do. +*/ + +void set_fastest_implementation(const std::string& algo, + Botan::RandomNumberGenerator& rng, + double ms = 30) + { + Botan::Default_Benchmark_Timer timer; + + Botan::Algorithm_Factory& af = Botan::global_state().algorithm_factory(); + + std::map results = + Botan::algorithm_benchmark(algo, ms, timer, rng, af); + + std::string fastest_provider = ""; + double best_res = 0; + + for(std::map::iterator r = results.begin(); + r != results.end(); ++r) + { + std::cout << r->first << " @ " << r->second << " MiB/sec\n"; + + if(fastest_provider == "" || r->second > best_res) + { + fastest_provider = r->first; + best_res = r->second; + } + } + + std::cout << "Using " << fastest_provider << "\n"; + + af.set_preferred_provider(algo, fastest_provider); + } + +int main(int argc, char* argv[]) + { + if(argc <= 1) + { + std::cout << "Usage: " << argv[0] << " ...\n"; + return 1; + } + + Botan::LibraryInitializer init; + Botan::AutoSeeded_RNG rng; + + const std::string hash = "SHA-1"; + + //set_fastest_implementation(hash, rng); + + // Here we intentionally use the 'old style' lookup interface + // which will also respect the provider settings. Or can use: + // global_state().algorithm_factory().make_hash_function(hash) + Botan::Pipe pipe( + new Botan::Hash_Filter(Botan::get_hash(hash)), + new Botan::Hex_Encoder + ); + + for(size_t i = 1; argv[i]; ++i) + { + std::ifstream in(argv[i]); + if(!in) + continue; + + pipe.start_msg(); + in >> pipe; + pipe.end_msg(); + + std::cout << argv[i] << " = " + << pipe.read_all_as_string(Botan::Pipe::LAST_MESSAGE) << "\n"; + + } + } -- cgit v1.2.3