Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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