Created
          March 7, 2017 13:02 
        
      - 
      
- 
        Save paroj/2154ad326f154d63fbfd6327bf83298c to your computer and use it in GitHub Desktop. 
    memory allocation performance
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <cstring> | |
| #include <cmath> | |
| #include <cstdio> | |
| #include <ctime> | |
| #include <cstdlib> | |
| #include <vector> | |
| #define NITER 100//5000000 | |
| int main() { | |
| clock_t t; | |
| t = clock(); | |
| for(size_t i = 0; i < NITER; i++) { | |
| char* buf = (char*)malloc(8*1024*1024); | |
| asm volatile ("" :: "g"(buf) : "memory"); | |
| free(buf); | |
| } | |
| printf("pure %fs\n", float(clock() - t)/CLOCKS_PER_SEC); | |
| t = clock(); | |
| for(size_t i = 0; i < NITER; i++) { | |
| char* buf = (char*)malloc(8*1024*1024); | |
| memset(buf, buf[0], 8*1024*1024); | |
| asm volatile ("" :: "g"(buf) : "memory"); | |
| free(buf); | |
| } | |
| printf("memset %fs\n", float(clock() - t)/CLOCKS_PER_SEC); | |
| t = clock(); | |
| std::vector<char> buf; | |
| for(size_t i = 0; i < NITER; i++) { | |
| buf.resize(8*1024*1024); | |
| asm volatile ("" :: "g"(&buf[0]) : "memory"); | |
| buf.clear(); | |
| } | |
| printf("vector %fs\n", float(clock() - t)/CLOCKS_PER_SEC); | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment