Julia vs C++ single dispatch performance comparison Please check it here: https://github.com/cdsousa/SingleDispatchJuliaVsCXX
Last active
February 24, 2017 17:40
-
-
Save cdsousa/f5d669fe3fba7cf848d8 to your computer and use it in GitHub Desktop.
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
cmake_minimum_required(VERSION 2.6) | |
project(subclassdispatchtests) | |
set(CMAKE_BUILD_TYPE Release) | |
add_definitions(-std=c++11) | |
add_executable(subclassdispatchtests subclassdispatchtests.cpp) |
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
#include <iostream> | |
#include <chrono> | |
#include <vector> | |
#include <memory> | |
using Clock = std::chrono::high_resolution_clock; | |
using std::chrono::microseconds; | |
struct AbstrType{ | |
virtual int f() = 0; | |
}; | |
struct ConcrType1: public AbstrType{ | |
int x; | |
ConcrType1(int x): x{x} {} | |
virtual int f() { return x; } | |
}; | |
struct ConcrType2: public AbstrType{ | |
int x; | |
ConcrType2(int x): x{x} {} | |
virtual int f() { return x; } | |
}; | |
int main(int argc, char **argv) { | |
int n = 100000; | |
std::vector<ConcrType1> arrconcr; | |
for(int i=0; i<n; i++){ | |
arrconcr.push_back(ConcrType1(i)); | |
} | |
std::vector<AbstrType*> arrabstr; | |
for(int i=0; i<n; i++){ | |
// alternate object type to inhibit branch prediction (?) | |
if(rand()%2){ | |
arrabstr.push_back(new ConcrType1(i)); | |
}else{ | |
arrabstr.push_back(new ConcrType2(i)); | |
} | |
} | |
auto t0 = Clock::now(); | |
for(auto& e : arrconcr) { e.f(); } | |
auto t1 = Clock::now(); | |
auto t2 = Clock::now(); | |
for(auto& p : arrabstr) { p->f(); } | |
auto t3 = Clock::now(); | |
std::cout << std::chrono::duration_cast<microseconds>(t1-t0).count() * 1e-6 << " seconds" << std::endl; | |
std::cout << std::chrono::duration_cast<microseconds>(t3-t2).count() * 1e-6 << " seconds" << std::endl; | |
for(auto p: arrabstr) { delete p; } | |
return 0; | |
} |
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
abstract AbstrType | |
type ConcrType1 <: AbstrType | |
x::Int | |
end | |
f(a::ConcrType1) = a.x | |
type ConcrType2 <: AbstrType | |
x::Int | |
end | |
f(a::ConcrType2) = a.x | |
function main() | |
println() | |
gc() | |
n = 100_000 | |
arrconcr = [ConcrType1(i) for i=1:n] | |
arrabstr = AbstrType[rand(Bool) ? ConcrType1(i) : ConcrType2(i) for i=1:n] | |
@time for a in arrconcr; f(a); end | |
@time for a in arrabstr; f(a); end | |
# Yet another experiment | |
@time for a in arrabstr | |
T = typeof(a) | |
if T === ConcrType1 | |
f(a::ConcrType1) | |
elseif T === ConcrType2 | |
f(a::ConcrType2) | |
else | |
f(a) | |
end | |
end | |
end | |
main() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment