Advertisement
cxzuk

fileRead

Jul 10th, 2020
1,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <benchmark/benchmark.h>
  6.  
  7. /*
  8. static int s_AllocCount = 0;
  9.  
  10. void* operator new(size_t size) {
  11.     s_AllocCount += 1;
  12.    
  13.     std::cout << "Allocating " << size << " bytes\n";
  14.    
  15.     return malloc(size);
  16. }*/
  17.  
  18. static std::string readFile(const std::string& filename) {
  19.     // Todo: needs better error handling
  20.     std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
  21.  
  22.     std::ifstream::pos_type fileSize = ifs.tellg();
  23.     if (fileSize < 0) return std::string{};
  24.  
  25.     ifs.seekg(0, std::ios::beg);
  26.  
  27.     std::vector<char> bytes(fileSize);
  28.     ifs.read(&bytes[0], fileSize);
  29.  
  30.     return std::string(&bytes[0], fileSize);
  31. }
  32.  
  33. void SomeFunction() {
  34.     auto source_text = readFile("Test Programs\\Bank Account\\one_m.inq");
  35.    
  36.     //std::cout << source_text.length() << '\n';
  37.    
  38. //  std::cout << s_AllocCount << " allocations\n";
  39.    
  40.     return;
  41. }
  42.  
  43. static void BM_SomeFunction(benchmark::State& state) {
  44.   // Perform setup here
  45.   for (auto _ : state) {
  46.     // This code gets timed
  47.     SomeFunction();
  48.   }
  49. }
  50. // Register the function as a benchmark
  51. BENCHMARK(BM_SomeFunction);
  52. // Run the benchmark
  53. BENCHMARK_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement