Skip to content

Instantly share code, notes, and snippets.

@avshabanov
Created December 22, 2013 01:13
Show Gist options
  • Save avshabanov/8077339 to your computer and use it in GitHub Desktop.
Save avshabanov/8077339 to your computer and use it in GitHub Desktop.
Virtual function check (C vs C++)
isis:cpp_vs_c alex$ gcc -O3 -std=c99 virt.c -o /tmp/v
isis:cpp_vs_c alex$ gcc -O3 virt.cpp -o /tmp/vpp
isis:cpp_vs_c alex$ /tmp/v
Ret = 100001000, Time: 0 sec 188086 000 msec
isis:cpp_vs_c alex$ /tmp/vpp
(C++) Ret = 100001000, Time: 0 sec 000001 000 msec
isis:cpp_vs_c alex$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define NANO_UNIT (1000000000LL)
static void printFormattedNanoTime(long long nanoTime, FILE * os) {
long long sec = nanoTime / NANO_UNIT;
long long nanos = (nanoTime - (sec * NANO_UNIT));
fprintf(os, "%lld sec %06lld %03lld msec", sec, nanos / 1000, nanos % 1000);
}
/*
* class Bar {
* public:
* int member;
* Bar(int member) { this.member = member; }
* virtual int add(int a, int b) { return this.member + a + b; }
* }
*/
struct BarVtbl {
int (* add)(void * this, int a, int b);
};
struct Bar {
struct BarVtbl * vtbl;
int member;
};
static int Bar_add(void * this, int a, int b) {
struct Bar * self = (struct Bar *) this;
return self->member + a + b;
}
static struct BarVtbl g_BarVtbl = {
&Bar_add
};
static struct Bar *
Bar_new(int member) {
struct Bar * b = malloc(sizeof(struct Bar));
b->vtbl = &g_BarVtbl;
b->member = member;
return b;
}
int main(int argc, char * argv[]) {
struct Bar * b = Bar_new(1000);
struct timeval start;
struct timeval stop;
int ret;
gettimeofday(&start, NULL);
for (int i = 0; i < 100000000; ++i) {
ret = b->vtbl->add(b, i, 1);
}
gettimeofday(&stop, NULL);
long long nanoTime = (stop.tv_sec - start.tv_sec) * NANO_UNIT + (stop.tv_usec - start.tv_usec) * 1000L;
printf("\nRet = %d, Time: ", ret);
printFormattedNanoTime(nanoTime, stdout);
printf("\n\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define NANO_UNIT (1000000000LL)
static void printFormattedNanoTime(long long nanoTime, FILE * os) {
long long sec = nanoTime / NANO_UNIT;
long long nanos = (nanoTime - (sec * NANO_UNIT));
fprintf(os, "%lld sec %06lld %03lld msec", sec, nanos / 1000, nanos % 1000);
}
class Bar {
private:
int member;
public:
Bar(int member) { this->member = member; }
virtual int add(int a, int b) {
return member + a + b;
}
};
int main(int argc, char * argv[]) {
Bar b(1000);
struct timeval start;
struct timeval stop;
int ret;
gettimeofday(&start, NULL);
for (int i = 0; i < 100000000; ++i) {
ret = b.add(i, 1);
}
gettimeofday(&stop, NULL);
long long nanoTime = (stop.tv_sec - start.tv_sec) * NANO_UNIT + (stop.tv_usec - start.tv_usec) * 1000L;
printf("\n(C++) Ret = %d, Time: ", ret);
printFormattedNanoTime(nanoTime, stdout);
printf("\n\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment