Last active
July 5, 2017 04:23
-
-
Save alphaKAI/89e53828703154ed2d281a0fac322222 to your computer and use it in GitHub Desktop.
mallocの簡単なベンチマーク(DとC)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #define testSize 4194304UL | |
| #define ulong unsigned long long | |
| //#define SHOW | |
| typedef struct test { | |
| ulong id; | |
| char buf[1024]; | |
| } Test; | |
| void test1() { | |
| Test** tests = (Test**)malloc(sizeof(Test*) * testSize); | |
| for (ulong idx = 0UL; idx < testSize; ++idx) { | |
| Test* test = (Test*)malloc(sizeof(Test)); | |
| test->id = idx; | |
| tests[idx] = test; | |
| } | |
| #ifdef SHOW | |
| for (ulong idx = 0UL; idx < testSize; ++idx) { | |
| Test* test = tests[idx]; | |
| printf("test->id : %lld\n", test->id); | |
| } | |
| #endif | |
| } | |
| void test2() { | |
| void* heap = malloc(sizeof(Test) * testSize); | |
| Test** tests = (Test**)malloc(sizeof(Test*) * testSize); | |
| for (ulong idx = 0; idx < testSize; ++idx) { | |
| Test* test = heap + idx * sizeof(Test); | |
| test->id = idx; | |
| tests[idx] = test; | |
| } | |
| #ifdef SHOW | |
| for (ulong idx = 0; idx < testSize; ++idx) { | |
| Test* test = tests[idx]; | |
| printf("test->id : %lld\n", test->id); | |
| } | |
| #endif | |
| } | |
| int main(int argc, const char *argv[]) { | |
| test1(); | |
| //test2(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import core.memory, | |
| std.stdio; | |
| struct Test { | |
| ulong id; | |
| char[1024] buf; | |
| } | |
| ulong testSize =1024UL^^2 * 4; | |
| enum SHOW = false; | |
| void test1() { | |
| Test*[] tests; | |
| tests.length = testSize; | |
| foreach (idx; 0..testSize) { | |
| Test* test = cast(Test*)GC.malloc(Test.sizeof); | |
| test.id = idx; | |
| tests[idx] = test; | |
| } | |
| static if (SHOW) { | |
| foreach (test; tests) { | |
| writeln("test.id : ", test.id); | |
| } | |
| } | |
| } | |
| void test2() { | |
| void* heap = GC.malloc(Test.sizeof * testSize); | |
| Test*[] tests; | |
| tests.length = testSize; | |
| foreach (idx; 0..testSize) { | |
| Test* test = cast(Test*)heap[(idx*Test.sizeof)..((idx+1)*Test.sizeof)]; | |
| test.id = idx; | |
| tests[idx] = test; | |
| } | |
| static if (SHOW) { | |
| foreach (test; tests) { | |
| writeln("test.id : ", test.id); | |
| } | |
| } | |
| } | |
| void main() { | |
| test1(); | |
| //test2(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment