zoukankan      html  css  js  c++  java
  • test speed of shared memory

    代码
    // sharedMem.cpp : Defines the entry point for the console application.
    //

    #include 
    "stdafx.h"

    #include 
    <windows.h>
    #include 
    <stdio.h>
    #include 
    <conio.h>
    #include 
    <tchar.h>
    #include 
    <vector>

    #define BUF_SIZE (8000*2000)

    TCHAR szName[]
    =TEXT("Global\\MyFileMappingObject");


    int server(char *exe)
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
       std::vector
    <char> vBuf(BUF_SIZE);

       hMapFile 
    = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    
    // use paging file
                     NULL,                    // default security 
                     PAGE_READWRITE,          // read/write access
                     0,                       // maximum object size (high-order DWORD) 
                     BUF_SIZE,                // maximum object size (low-order DWORD)  
                     szName);                 // name of mapping object
     
       
    if (hMapFile == NULL) 
       { 
          _tprintf(TEXT(
    "Could not create file mapping object (%d).\n"), 
                 GetLastError());
          
    return 1;
       }
       pBuf 
    = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                            FILE_MAP_ALL_ACCESS, // read/write permission
                            0,                   
                            
    0,                   
                            BUF_SIZE);           
     
       
    if (pBuf == NULL) 
       { 
          _tprintf(TEXT(
    "Could not map view of file (%d).\n"), 
                 GetLastError()); 

           CloseHandle(hMapFile);

          
    return 1;
       }

       DWORD dwTm 
    = GetTickCount();

       CopyMemory((PVOID)pBuf, 
    &vBuf[0], vBuf.size());
       
       DWORD dwDiff 
    = GetTickCount()-dwTm;

       printf(
    "Copy from user to shared memory use time %d\n", dwDiff);

       
    char cmd[1000];
       sprintf(cmd,
    "%s c", exe);
       system(cmd);
       getch();

       UnmapViewOfFile(pBuf);
       CloseHandle(hMapFile);

       
    return 0;
    }

    int client()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
        std::vector
    <char> vBuf(BUF_SIZE);

       hMapFile 
    = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   
    // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object 
     
       
    if (hMapFile == NULL) 
       { 
          _tprintf(TEXT(
    "Could not open file mapping object (%d).\n"), 
                 GetLastError());
          
    return 1;
       } 
     
       pBuf 
    = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,                    
                   
    0,                    
                   BUF_SIZE);                   
     
       
    if (pBuf == NULL) 
       { 
          _tprintf(TEXT(
    "Could not map view of file (%d).\n"), 
                 GetLastError()); 

          CloseHandle(hMapFile);

          
    return 1;
       }

       DWORD dwTm 
    = GetTickCount();

       CopyMemory(
    &vBuf[0], (PVOID)pBuf, vBuf.size());

       DWORD dwDiff 
    = GetTickCount()-dwTm;

       printf(
    "Copy from shared memory to user use time %d\n", dwDiff);
       
       UnmapViewOfFile(pBuf);
        
       CloseHandle(hMapFile);
     
       
    return 0;
    }


    int _tmain(int argc, char *argv[])
    {
        
    if(argc>1)
        {
            
    if(argv[1][0]=='c')
                client();
        }
        
    else
            server(argv[
    0]);

        
    return 0;
    }


  • 相关阅读:
    [POJ1176]Party Lamps(DFS,规律)
    [POJ1598]Excuses, Excuses!(模拟)
    [POJ2192]Zipper(DFS,剪枝)
    [POJ2157]Maze(DFS)
    [POJ1950]Dessert(DFS)
    [HIHO1353]满减优惠(状压,枚举)
    [HIHO1177]顺子(暴力,枚举)
    [HIHO1152]Lucky Substrings(暴力,枚举)
    计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
    计蒜客 28206.Runway Planning (BAPC 2014 Preliminary ACM-ICPC Asia Training League 暑假第一阶段第一场 F)
  • 原文地址:https://www.cnblogs.com/cutepig/p/1768010.html
Copyright © 2011-2022 走看看