zhyncs opened a new issue, #13179:
URL: https://github.com/apache/tvm/issues/13179

   @areusch 
   Hi Andrew Reusch, I write a simple benchmark to test the speed between C++ 
ifstream and fread when load the params data. The code snippet is below. And I 
use the `read_all_or_die` function for fread case, which was written by you. In 
my test case, the params data size is larger than 1MB. Maybe we can remove the 
1MB limit in the `apps/bundle_deploy/test.cc` for workaround. If it's OK, I 
will send a PR to remove this.
   
https://github.com/apache/tvm/blob/24e89befd6c2f45fe47512de5d1445d8ea809a98/apps/bundle_deploy/test.cc#L46-L49
   
   ```c++
   #include <benchmark/benchmark.h>
   #include <sys/stat.h>
   
   #include <fstream>
   #include <iostream>
   
   static void BM_read_file(benchmark::State& state) {
     for (auto _ : state) {
       std::ifstream json_in("/Users/zhyncs/Downloads/0_1.params", 
std::ios::in);
       std::string json_data((std::istreambuf_iterator<char>(json_in)),
                             std::istreambuf_iterator<char>());
       json_in.close();
     }
   }
   
   BENCHMARK(BM_read_file);
   
   static char* read_all_or_die(const char* name, const char* file_path, 
size_t* out_size) {
     struct stat st;
     if (stat(file_path, &st)) {
       char err[1024];
       snprintf(err, sizeof(err), "%s: statting file", name);
       perror(err);
       abort();
     }
   
     if (out_size != nullptr) {
       *out_size = st.st_size;
     }
   
     char* data = (char*)malloc(st.st_size);
     FILE* fp = fopen(file_path, "rb");
     size_t bytes_to_read = st.st_size;
     size_t bytes_read = 0;
     while (bytes_read < bytes_to_read) {
       size_t this_round = fread(data, 1, st.st_size, fp);
       if (this_round == 0) {
         if (ferror(fp)) {
           char err[1024];
           snprintf(err, sizeof(err), "%s: error during read", name);
           perror(err);
         } else if (feof(fp)) {
           std::cerr << name << ": file is shorter than its stat size (" << 
bytes_read << " v "
                     << st.st_size << ")" << std::endl;
         } else {
           std::cerr << name << ": fread stopped returning data" << std::endl;
         }
         abort();
       }
       bytes_read += this_round;
     }
   
     fclose(fp);
     return data;
   }
   
   static void BM_read_file_v2(benchmark::State& state) {
     for (auto _ : state) {
       read_all_or_die("params_data", "/Users/zhyncs/Downloads/0_1.params", 
nullptr);
     }
   }
   
   BENCHMARK(BM_read_file_v2);
   
   BENCHMARK_MAIN();
   
   ```
   
   The result is below.
   ```
   ----------------------------------------------------------
   Benchmark                Time             CPU   Iterations
   ----------------------------------------------------------
   BM_read_file    3246447709 ns   3244741000 ns            1
   BM_read_file_v2   24013239 ns     23994069 ns           29
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to