C ++ commonly used functions

1, calculate the variable size

_msize #Calculate the new memory size in bytes

sizeof #Calculate the size of the data type, such as int is 4 bytes, mat is 96 bytes, vector is 32 bytes no matter how big

strlen #how many elements in the array

2, printf format string parameter

Print double:% f

Print long: ubuntu like this:% ld (because in ubuntu, long has 8 bytes)

 

 

 3. Time related

Available on windows:

#include <time.h> 
#include <windows.h> // The time header clock_t start, ends; // clock_t is actually a long start = clock (); // From the start of the program process to the program call The number of milliseconds between clock () function Sleep ( 50 ); ends = clock (); cout << ends-start << endl; // This function is often used to calculate the time difference

 It is not recommended to use clock on ubuntu. It is said that the timing is not accurate. You can refer to the following code:

#include <sys / time.h> 
#include <stdio.h> 
#include <unistd.h>
 using  namespace std;
 // The following seems to be the built-in time structure of sys / time.h
 // struct timeval {
 //       long tv_sec ;
 //       long tv_usec;
 // };
 // struct timezone {
 //       int tz_minutesweat;
 //       int tz_dsttime;
 // }; 

int main () {
   struct timeval t1, t2; 
  gettimeofday ( & t1, NULL);
   int suma ;
   for ( int a = 0; a < 10000 ; a ++ ) { 
      suma = suma + a; 
  }; 
  sleep ( 10 ); 
  gettimeofday ( & t2, NULL);
   double timegap = (t2.tv_sec-t1.tv_sec) * 1000 + (t2.tv_usec-t1. tv_usec) * 1.0 / 1000 ;
    // Remember to multiply by 1.0 otherwise there are no decimal places 
  printf ( " tv1:% ld,% ld \ n " , t1.tv_sec, t1.tv_usec); 
  printf ( " tv2:% ld ,% ld \ n " , t2.tv_sec, t2.tv_usec); 
  printf ( " timegap:% f \ n " , timegap); 
}

Compile command:

gcc timetest.cpp -o out -std=c++11 -lstdc++

 

Guess you like

Origin www.cnblogs.com/0-lingdu/p/12407762.html