Performance (speed) Comparison of malloc, new, virtualalloc, and heapalloc

Source: Internet
Author: User

Here we compare the performance of C ++ Code Compiled by VC ++.

I used vc6.0 for testing.

I will not describe these usage cases.

 

I wrote a simple test code.

The test result is:

Malloc: 390
"New: 391"
Virtualalloc: 454
Heapalloc: 47

Obviously, heapalloc is the fastest allocation speed, followed by malloc. New is similar to malloc, and virtualalloc is the slowest (I told Xiaoqiang that it was the fastest in the past)

I tracked it.

New calls this code.

 

  1. Void * _ cdecl _ nh_malloc (
  2. Size_t nsize,
  3. Int nhflag
  4. )
  5. {
  6. Return _ nh_malloc_dbg (nsize, nhflag, _ normal_block, null, 0 );
  7. }

The malloc function is as follows:

  1. _ Cribd void * _ cdecl malloc (
  2. Size_t nsize
  3. )
  4. {
  5. Return _ nh_malloc_dbg (nsize, _ newmode, _ normal_block, null, 0 );
  6. }

Obviously, new and malloc eventually call the same _ nh_malloc_dbg, but new has an additional callback function call.

Continue and find that the final call is return heapalloc (_ crtheap, 0, size );

The truth is that

 

Virtualalloc cannot be tracked. If virtual memory is allocated, it may be slow.

Let's take a closer look at the book windows core programming!

Thank you! Welcome!

 

The test code is as follows:

  1. /*************************************** ***************************
  2. *
  3. * Copyright (c) 2008, xxxx
  4. * All Rights Reserved.
  5. *
  6. * File name: Main. cpp
  7. * Abstract: test the memory application speed.
  8. *
  9. * Version: 1.0
  10. * Author: Wu huiran
  11. * Completion date:
  12. *
  13. * Replaced version:
  14. * Original Author:
  15. * Completion date:
  16. *
  17. **************************************** **************************/
  18. # Include <iostream>
  19. # Include <windows. h>
  20. Using namespace STD;
  21. Int main (INT argc, char * argv [])
  22. {
  23. Int I = 0;
  24. DWORD dw1 = 0, dw2 = 0, dw3 = 0, dw4 = 0;
  25. DWORD dwstart = 0;
  26. DWORD dwend = 0;
  27. For (Int J = 0; j <10; j ++)
  28. {
  29. Dwstart =: gettickcount ();
  30. For (I = 0; I <20000; I ++)
  31. {
  32. Char * pdest1 = (char *) malloc (4096 );
  33. Free (pdest1 );
  34. }
  35. Dwend =: gettickcount ();
  36. Cout <"malloc 10000 times memory block size, time consumed" <dwend-dwstart <Endl;
  37. Dw1 + = dwend-dwstart;
  38. Dwstart =: gettickcount ();
  39. For (I = 0; I <20000; I ++)
  40. {
  41. Char * pdest2 = new char [4096];
  42. Delete pdest2;
  43. }
  44. Dwend =: gettickcount ();
  45. Cout <"10000 new 4096 memory blocks, time consumed" <dwend-dwstart <Endl;
  46. Dw2 + = dwend-dwstart;
  47. Dwstart =: gettickcount ();
  48. For (I = 0; I <20000; I ++)
  49. {
  50. Void * pmem =: virtualalloc (null, 4096, mem_reserve | mem_commit, page_readwrite );
  51. : Virtualfree (pmem, 0, mem_release );
  52. }
  53. Dwend =: gettickcount ();
  54. Cout <"virtualalloc 10000 times memory block size, time consumed" <dwend-dwstart <Endl;
  55. Dw3 + = dwend-dwstart;
  56. Handle hheap =: heapcreate (heap_no_serialize, 0, 0 );
  57. Dwstart =: gettickcount ();
  58. For (I = 0; I <20000; I ++)
  59. {
  60. Void * pmem2 =: heapalloc (hheap, heap_no_serialize, 4096 );
  61. : Heapfree (hheap, heap_no_serialize, pmem2 );
  62. }
  63. Dwend =: gettickcount ();
  64. Cout <"heapalloc 10000 memory blocks per 4096 times, time consumed" <dwend-dwstart <Endl;
  65. Dw4 + = dwend-dwstart;
  66. }
  67. Cout <"malloc:" <dw1 <Endl;
  68. Cout <"New:" <dw2 <Endl;
  69. Cout <"virtualalloc:" <dw3 <Endl;
  70. Cout <"heapalloc:" <dw4 <Endl;
  71. Return 0;
  72. }

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.