比较复制内存缓冲区的已用时间:memcpy vs ForLoopIndexCopying

时间:2018-02-15 21:28:40

标签: c++ visual-studio-2017 memcpy

我试图理解复制时间内缓冲区的“memcpy”和“for-loop-index-copying”之间的区别。

results:  
          CopyingType  |  MemoryType  |  RunMode  |  Elapsed Time(ms)
          -----------------------------------------------------------
             memcpy    |    stack     |   Debug   |          x   
       forLoopIndexing |    stack     |   Debug   |        30x
             memcpy    |    stack     |   Release |          0   
       forLoopIndexing |    stack     |   Release |          0
             memcpy    |    heap      |   Release |          0   
       forLoopIndexing |    heap      |   Release |       2000  

这是我的代码运行...也许我做错了什么?

似乎奇怪的是复制500,000字节的缓冲区100,000次,或者至少不到机器的分辨率......在我的情况下是16ms。

#include "stdafx.h"
#include <windows.h>
#include <iostream>

int main()
{
  long baseTime;

  const long packetLength = 500000;
  //char packet1[packetLength];//stack
  //char packet2[packetLength];//stack
  char *packet1 = (char*)calloc(packetLength, sizeof(char));//heap
  char *packet2 = (char*)calloc(packetLength, sizeof(char));//heap
  memset(packet1, 0, packetLength);//init
  memset(packet2, 0, packetLength);//init
  long NumPackets = 100000;
  long NumRuns = 10;


  for (long k = 0; k < NumRuns; k++)
  {
    //create packet
    printf("\npacket1:\n");
    for (long i = 0; i < packetLength; i++) {
      packet1[i] = (char)(i % 26 + 65);
    }

    printf("\nk:%d\n", k);

    //index copy
    baseTime = GetTickCount();
    long ii = 0;
    for (long j = 0; j < NumPackets; j++) {
      for (long i = 0; i < packetLength; i++) {
        packet2[i] = packet1[i];
      }
    }
    printf("Time(IndexCopy): %ld\n", GetTickCount() - baseTime);


    //memcpy
    memset(packet2, 0, packetLength);//reset

    baseTime = GetTickCount();
    for (long j = 0; j < NumPackets; j++) {
      memcpy(packet2, packet1, packetLength); //Changed via PaulMcKenzie.
    }
    printf("Time(memcpy): %ld\n", GetTickCount() - baseTime);


    //printf("\npacket2\n");
    for (long i = 0; i < packetLength; i++) {
      //printf("%c", packet2[i]);
    }

  }
  int iHalt;
  scanf_s("%d", &iHalt);

  return 0;
}

通过改变......新表

          CopyingType  |  MemoryType  |  RunMode  |  Elapsed Time(ms)
          -----------------------------------------------------------
             memcpy    |    stack     |   Debug   |          x   
       forLoopIndexing |    stack     |   Debug   |        50x
             memcpy    |    stack     |   Release |          0   
       forLoopIndexing |    stack     |   Release |          0
             memcpy    |    heap      |   Release |       2000   
       forLoopIndexing |    heap      |   Release |       2000  

1 个答案:

答案 0 :(得分:0)

  

也许我做错了什么?

在使用memcpy的代码中,您做错了,更重要的是。

const long packetLength = 500000;
char *packet1 = (char*)calloc(packetLength, sizeof(char));
char *packet2 = (char*)calloc(packetLength, sizeof(char));    
//...
for (long j = 0; j < NumPackets; j++) {
      memcpy(packet2, packet1, sizeof(packet2)); // <-- Incorrect
    }

sizeof(packet2)sizeof(char *)相同,很可能是4或8。

您想要的不是sizeof(char *),而是要复制的实际字节数。

for (long j = 0; j < NumPackets; j++) {
      memcpy(packet2, packet1, packetLength);
    }