Test the speed of the CopyMemory function

Source: Internet
Author: User
Tags bool error handling
Introduction

The function of the CopyMemory function: Copying a piece of memory data from one location to another. Of these, two blocks of memory cannot have overlapping parts. test Environment Windows version: Windows 7 Ultimate SP1 system type: 64-bit processor: intel® Pentium (Pentium) dual core E5400 @ 2.70GHz Memory: 4GB memory stripe: Two memory technology DDR3 1333MHZ 2GB compiler: Microsoft Visual Studio Ultimate Update 5 program Template: Visual C + + Win32 Console Application test content

Test the speed at which the CopyMemory function replicates, that is, how long it takes to replicate a certain size of memory. Considering the existence of replication in different sizes, different algorithms may be used, so the memory of different sizes needs to be tested. Test Code implement test requirements only, no error handling

    ConsoleApplication1.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <Windows.h> #include <iostream> int _tmain (int argc, _tchar*
        Argv[]) {size_t len = 1;
        void * a = nullptr;
        void * B = nullptr;
        Large_integer freq, begin_t, end_t;
        Double time = 0;

        BOOL QRF = QueryPerformanceFrequency (&freq);

            for (len = 1; Len <= 536870912; Len *= 2) {a = malloc (len);//Get memory B = malloc (len);

            ZeroMemory (A, Len);//Use Memory ZeroMemory (b, Len);

            Std::cout << len << "bytes, time-consuming";
            QRF = QueryPerformanceCounter (&begin_t);
            CopyMemory (A, b, Len);

            QRF = QueryPerformanceCounter (&end_t);
            Free (a);//release memory (b);
            Time = (double) (End_t.quadpart-begin_t.quadpart); Time/= (double) freq.
            QuadPart; Std::cout << std::fixed <&Lt
        Time << Std::endl;
        } system ("Pause");
    return 0; }
with error handling
    ConsoleApplication1.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <Windows.h> #include <iostream> int _tmain (int argc, _tchar*
        Argv[]) {size_t len = 1;
        void * a = nullptr;
        void * B = nullptr;
        Large_integer freq, begin_t, end_t;
        Double time = 0;
        BOOL QRF = QueryPerformanceFrequency (&freq); if (QRF = = FALSE)//Hardware support QueryPerformanceFrequency {std::cout << "QRF = = True" << GetLast
            Error () << Std::endl;
            System ("pause");
        return 0; } for (len = 1; Len <= 536870912; Len *= 2) {a = malloc (len);//Get memory B = Mallo
            C (LEN); if (a = = nullptr)//If a gets memory failure {std::cout << "a = = nullptr" << GetLastError () <
                < Std::endl;
                System ("pause");
            return 0; } if (b = = nullptr)//if B receivesFailed to fetch memory {GetLastError ();
                Std::cout << "b = = nullptr" << GetLastError () << Std::endl;
                System ("pause");
            return 0;

            } zeromemory (A, Len);//Use Memory ZeroMemory (b, Len);

            Std::cout << len << "bytes, time-consuming";
            QRF = QueryPerformanceCounter (&begin_t); if (QRF = = FALSE) {std::cout << "Qrf1 = True" << GetLastError () << Std::en
                dl
                System ("pause");
            return 0;
            } copymemory (A, b, Len);
            QRF = QueryPerformanceCounter (&end_t); if (QRF = = FALSE) {std::cout << "Qrf2 = True" << GetLastError () << Std::en
                dl
                System ("pause");
            return 0;
            } free (a);//Freeing up memory (b); Time = (double) (end_t.qUadpart-begin_t.quadpart); Time/= (double) freq.
            QuadPart;
        Std::cout << std::fixed << time << Std::endl;
        } system ("Pause");
    return 0; }
Test Results
Memory Size Unit time-consuming (seconds)
1 Bytes 0.000001
2 Bytes 0.000000
4 Bytes 0.000000
8 Bytes 0.000000
16 Bytes 0.000000
32 Bytes 0.000000
64 Bytes 0.000000
128 Bytes 0.000000
256 Bytes 0.000000
512 Bytes 0.000001
1024 Bytes 0.000001
2048 Bytes 0.000000
4096 Bytes 0.000001
8192 Bytes 0.000006
16384 Bytes 0.000010
32768 Bytes 0.000018
65536 Bytes 0.000019
131072 Bytes 0.000059
262144 Bytes 0.000143
524288 Bytes 0.000275
1048576 Bytes 0.000702
2097152 Bytes 0.001375
4194304 Bytes 0.003046
8388608 Bytes 0.005909
16777216 Bytes 0.015223
33554432 Bytes 0.025018
67108864 Bytes 0.051761
134217728 Bytes 0.096383
268435456 Bytes 0.194531
536870912 Bytes 0.410401
Test Notes The size limit of the memory that can be used for testing

The system restricts the maximum memory that the program requests to use malloc. Windows under 32-bit programs if you simply see the address space can have about 4G of memory available, but in fact, the system will leave the address of 2G to the kernel, so the program can use the maximum 2G of memory. With the exception of other costs, only about 1.9G of memory can be applied to malloc.
The test requires two pieces of the same size of memory, one as the source memory, one for the purpose memory, so the maximum memory can be copied at the test is about 0.9GB. the role of ZeroMemory in code

Malloc allocates a virtual address (not physical memory, even if physical memory is 0.5G), that is, allocating only the "right to allocate physical memory in the future", and only if it is actually used will the page table begin to correlate physical memory. The program uses the ZeroMemory function to achieve true use.
If you do not use ZeroMemory, the copymemory takes a longer time and the test results are as follows:

Memory Size Unit time-consuming (seconds)
1 Bytes 0.000000
2 Bytes 0.000000
4 Bytes 0.000000
8 Bytes 0.000000
16 Bytes 0.000000
32 Bytes 0.000000
64 Bytes 0.000001
128 Bytes 0.000000
256 Bytes 0.000000
512 Bytes 0.000001
1024 Bytes 0.000001
2048 Bytes 0.000000
4096 Bytes 0.000001
8192 Bytes 0.000002
16384 Bytes 0.000008
32768 Bytes 0.000008
65536 Bytes 0.000013
131072 Bytes 0.000025
262144 Bytes 0.000127
524288 Bytes 0.000603
1048576 Bytes 0.001112
2097152 Bytes 0.002571
4194304 Bytes 0.005079
8388608 Bytes 0.009425
16777216 Bytes 0.022391
33554432 Bytes 0.042390
67108864 Bytes 0.081809
134217728 Bytes 0.159133
268435456 Bytes 0.332674
536870912 Bytes 0.652054

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.