Created
September 26, 2011 18:13
-
-
Save gizmomogwai/1242911 to your computer and use it in GitHub Desktop.
calloverhead of methods of structs and classes
This file contains 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 std.datetime; | |
import std.stdio : writeln; | |
struct HStruct { | |
size_t c; | |
void add() { | |
c = c % 3 +1; | |
} | |
size_t get() { | |
return c; | |
} | |
} | |
final class HClass { | |
size_t c; | |
final void add() { | |
c = c % 3 +1; | |
} | |
final size_t get() { | |
return c; | |
} | |
} | |
int main(string[] args) { | |
auto sw = StopWatch(AutoStart.no); | |
{ | |
sw.start(); | |
HStruct h; | |
for (size_t i = 0; i<1_000_000; ++i) { | |
for (size_t j = 0; j<1_000; ++j) { | |
h.add(); | |
} | |
} | |
sw.stop(); | |
writeln("time with struct: ", sw.peek().msecs, " for ", h.get()); | |
} | |
{ | |
auto h = new HClass(); | |
sw.start(); | |
for (size_t i = 0; i<1_000_000; ++i) { | |
for (size_t j = 0; j<1_000; ++j) { | |
h.add(); | |
} | |
} | |
sw.stop(); | |
writeln("time with class: ", sw.peek().msecs, " for ", h.get()); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment