Skip to content

Instantly share code, notes, and snippets.

@gizmomogwai
Created September 26, 2011 18:13
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 gizmomogwai/1242911 to your computer and use it in GitHub Desktop.
Save gizmomogwai/1242911 to your computer and use it in GitHub Desktop.
calloverhead of methods of structs and classes
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