时间: 20181022 ~ 20181028

新闻


资源推荐

  • 知乎: 为什么 Python 不支持函数重载?其他函数大部分都支持的?

  • 精确测量时间

    • c, 跨平台, ms(millisecond)

      https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html#CPU-Time

      #include <time.h>
      clock_t start, end;
      double cpu_time_used;
      
      start = clock();
      // do something
      end = clock();
      cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
      
    • c, Windows, us(microsecond)

      https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx

      QPF获取CPU频率 freq, QPC获取当前tick

      (end_tick - start_tick) / freq = elapsed 秒, x10^6, 就是以 us 的形式显式

      #include <stdio.h>
      #include <windows.h>
      
      //获取当前时间,精度1微秒
      long GetCPUTime(){
          static LARGE_INTEGER li = {0};
          LARGE_INTEGER linow = {0};
          if (li.QuadPart == 0){
              QueryPerformanceFrequency(&li);
          }
          QueryPerformanceCounter(&linow);
          return linow.QuadPart * 1000000 / li.QuadPart;
      }
      
      int main(){
          long long starttime = GetCPUTime();
      
          // do something
          for(int i = 0; i < 0x3fffff0; i++){}
      
          long long endtime = GetCPUTime();
      
          long long elapsed =  endtime - starttime;
      
          printf_s("%ld\n", elapsed);
          return 0;
      }
      
    • C++11, 跨平台, us(microsecond)

      https://stackoverflow.com/questions/23615776/measure-time-milliseconds-or-microseconds-for-windows-c https://en.cppreference.com/w/cpp/chrono https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now

      #include <iostream>
      #include <chrono>
      #include <iomanip>
      using namespace std;
      
      int main(){
          auto start = chrono::high_resolution_clock::now();
      			
          for(int i = 0; i < 0x3fffff0; i++){}
      			
          auto end = chrono::high_resolution_clock::now();
      			
          chrono::duration<double> elapsed = (end-start);
      			
          cout<<setprecision(9)<<elapsed.count()<<'\n';   // 在我的PC上可以精确到 0.1us, 更高的精度则依机器和系统而定
      }
      
  • online plot, Desmos

    online plot, plot.ly


其他

本文地址:https://jjayyyyyyy.github.io/2018/10/28/weekly_20181022_20181028.html

(END)


相关阅读