Skip to content

Instantly share code, notes, and snippets.

@shirosaki
Created January 6, 2012 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirosaki/1569002 to your computer and use it in GitHub Desktop.
Save shirosaki/1569002 to your computer and use it in GitHub Desktop.
diff --git a/samples/mem_alloc.cpp b/samples/mem_alloc.cpp
index fb04964..4ee16ec 100644
--- a/samples/mem_alloc.cpp
+++ b/samples/mem_alloc.cpp
@@ -17,6 +17,9 @@
#include <stdexcept>
#if defined (_WIN32)
+# undef __MSVCRT_VERSION__
+# define __MSVCRT_VERSION__ 0x0700
+# include <malloc.h>
# include <windows.h>
#else
# include <cstdlib>
@@ -68,6 +71,49 @@ int main(int argc, char* argv[])
buf = NULL;
#if defined(_WIN32)
+ int i = 0;
+ while (i < 3) {
+ t_us.start();
+ buf = static_cast<wchar_t*>(__mingw_aligned_malloc(BUF_SIZE, 0x4000));
+ rv = t_us.stop();
+ if (buf == NULL) throw runtime_error("[ERROR] __mingw_aligned_malloc() failed.");
+ cout << left
+ << setw(32)
+ << "__mingw_aligned_malloc() time: "
+ << rv << endl;
+
+ t_us.start();
+ __mingw_aligned_free(static_cast<void*>(buf));
+ rv = t_us.stop();
+ if (buf == NULL) throw runtime_error("[ERROR] __mingw_aligned_free() failed.");
+ cout << left
+ << setw(32)
+ << "__mingw_aligned_free() time: "
+ << rv << endl;
+ buf = NULL;
+
+ t_us.start();
+ buf = static_cast<wchar_t*>(_aligned_malloc(BUF_SIZE, 0x4000));
+ rv = t_us.stop();
+ if (buf == NULL) throw runtime_error("[ERROR] _aligned_malloc() failed.");
+ cout << left
+ << setw(32)
+ << "_aligned_malloc() time: "
+ << rv << endl;
+
+ t_us.start();
+ _aligned_free(static_cast<void*>(buf));
+ rv = t_us.stop();
+ if (buf == NULL) throw runtime_error("[ERROR] _aligned_free() failed.");
+ cout << left
+ << setw(32)
+ << "_aligned_free() time: "
+ << rv << endl;
+ buf = NULL;
+
+ i++;
+ }
+
// Win32 HeapAlloc()
HANDLE heap = ::HeapCreate(0, 2*BUF_SIZE, 4*BUF_SIZE);
if (heap == NULL) throw runtime_error("[ERROR] HeapCreate() failed.");
@shirosaki
Copy link
Author

C:\devruby\tma\build\samples>mem_alloc.exe
malloc() time:    5.51788
free() time:      1.89935
calloc() time:    14.4366
__mingw_aligned_malloc() time:  8.99002
__mingw_aligned_free() time:    4.96609
_aligned_malloc() time:         8.64092
_aligned_free() time:           4.24539
__mingw_aligned_malloc() time:  6.55764
__mingw_aligned_free() time:    4.49313
_aligned_malloc() time:         6.78286
_aligned_free() time:           4.22287
__mingw_aligned_malloc() time:  6.59518
__mingw_aligned_free() time:    4.06146
_aligned_malloc() time:         6.58392
_aligned_free() time:           4.60199
HeapAlloc() time: 2.79648
HeapFree() time:  1.85055

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment