Last active
July 10, 2020 22:02
-
-
Save sizmailov/384312f7e301037bc8657e21faa48543 to your computer and use it in GitHub Desktop.
__hash__ check/fix benchmark
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
2020-07-10T23:28:00+03:00 | |
Running ./benchmark_def | |
Run on (4 X 3600 MHz CPU s) | |
CPU Caches: | |
L1 Data 32 KiB (x4) | |
L1 Instruction 32 KiB (x4) | |
L2 Unified 256 KiB (x4) | |
L3 Unified 6144 KiB (x1) | |
Load Average: 0.58, 0.83, 0.95 | |
-------------------------------------------------------------------------------- | |
Benchmark Time CPU Iterations | |
-------------------------------------------------------------------------------- | |
Fixture/def_foo 675737 ns 675602 ns 7154 | |
Fixture/def_foo 643496 ns 643469 ns 7154 | |
Fixture/def_foo 643357 ns 643335 ns 7154 | |
Fixture/def_foo 644410 ns 644391 ns 7154 | |
Fixture/def_foo 648751 ns 648720 ns 7154 | |
Fixture/def_foo 646038 ns 646017 ns 7154 | |
Fixture/def_foo 675606 ns 674058 ns 7154 | |
Fixture/def_foo 689514 ns 689480 ns 7154 | |
Fixture/def_foo 685113 ns 684511 ns 7154 | |
Fixture/def_foo 654920 ns 654886 ns 7154 | |
Fixture/def_foo_mean 660694 ns 660447 ns 10 | |
Fixture/def_foo_median 651836 ns 651803 ns 10 | |
Fixture/def_foo_stddev 18642 ns 18416 ns 10 | |
Fixture/def__eq__ 687740 ns 687712 ns 8294 | |
Fixture/def__eq__ 679903 ns 679881 ns 8294 | |
Fixture/def__eq__ 692623 ns 692599 ns 8294 | |
Fixture/def__eq__ 679187 ns 679160 ns 8294 | |
Fixture/def__eq__ 678909 ns 678880 ns 8294 | |
Fixture/def__eq__ 679786 ns 679756 ns 8294 | |
Fixture/def__eq__ 680223 ns 680200 ns 8294 | |
Fixture/def__eq__ 682308 ns 682283 ns 8294 | |
Fixture/def__eq__ 696546 ns 696520 ns 8294 | |
Fixture/def__eq__ 690322 ns 690292 ns 8294 | |
Fixture/def__eq___mean 684755 ns 684728 ns 10 | |
Fixture/def__eq___median 681266 ns 681241 ns 10 | |
Fixture/def__eq___stddev 6504 ns 6504 ns 10 | |
Fixture/def_foo_with_hash_fix 682190 ns 682162 ns 7550 | |
Fixture/def_foo_with_hash_fix 695269 ns 695245 ns 7550 | |
Fixture/def_foo_with_hash_fix 690788 ns 690756 ns 7550 | |
Fixture/def_foo_with_hash_fix 703205 ns 703177 ns 7550 | |
Fixture/def_foo_with_hash_fix 701367 ns 701328 ns 7550 | |
Fixture/def_foo_with_hash_fix 708367 ns 708334 ns 7550 | |
Fixture/def_foo_with_hash_fix 705088 ns 705061 ns 7550 | |
Fixture/def_foo_with_hash_fix 697820 ns 697790 ns 7550 | |
Fixture/def_foo_with_hash_fix 696740 ns 696708 ns 7550 | |
Fixture/def_foo_with_hash_fix 692154 ns 692125 ns 7550 | |
Fixture/def_foo_with_hash_fix_mean 697299 ns 697269 ns 10 | |
Fixture/def_foo_with_hash_fix_median 697280 ns 697249 ns 10 | |
Fixture/def_foo_with_hash_fix_stddev 7720 ns 7720 ns 10 | |
Fixture/def__eq__with_hash_fix 664665 ns 664644 ns 8025 | |
Fixture/def__eq__with_hash_fix 666627 ns 666601 ns 8025 | |
Fixture/def__eq__with_hash_fix 668808 ns 668782 ns 8025 | |
Fixture/def__eq__with_hash_fix 674010 ns 673413 ns 8025 | |
Fixture/def__eq__with_hash_fix 985554 ns 794555 ns 8025 | |
Fixture/def__eq__with_hash_fix 948418 ns 768318 ns 8025 | |
Fixture/def__eq__with_hash_fix 676578 ns 676270 ns 8025 | |
Fixture/def__eq__with_hash_fix 688799 ns 688561 ns 8025 | |
Fixture/def__eq__with_hash_fix 675710 ns 675533 ns 8025 | |
Fixture/def__eq__with_hash_fix 673838 ns 673812 ns 8025 | |
Fixture/def__eq__with_hash_fix_mean 732301 ns 695049 ns 10 | |
Fixture/def__eq__with_hash_fix_median 674860 ns 674672 ns 10 | |
Fixture/def__eq__with_hash_fix_stddev 124177 ns 46414 ns 10 | |
# # # # Second run # # # # # # | |
2020-07-10T23:33:51+03:00 | |
Running ./benchmark_def | |
Run on (4 X 3600 MHz CPU s) | |
CPU Caches: | |
L1 Data 32 KiB (x4) | |
L1 Instruction 32 KiB (x4) | |
L2 Unified 256 KiB (x4) | |
L3 Unified 6144 KiB (x1) | |
Load Average: 0.36, 0.79, 0.93 | |
-------------------------------------------------------------------------------- | |
Benchmark Time CPU Iterations | |
-------------------------------------------------------------------------------- | |
Fixture/def_foo 685595 ns 685490 ns 7122 | |
Fixture/def_foo 654087 ns 654063 ns 7122 | |
Fixture/def_foo 651341 ns 651315 ns 7122 | |
Fixture/def_foo 650846 ns 650825 ns 7122 | |
Fixture/def_foo 652360 ns 652336 ns 7122 | |
Fixture/def_foo 651960 ns 651939 ns 7122 | |
Fixture/def_foo 655237 ns 655212 ns 7122 | |
Fixture/def_foo 655527 ns 655506 ns 7122 | |
Fixture/def_foo 653194 ns 653172 ns 7122 | |
Fixture/def_foo 655713 ns 655690 ns 7122 | |
Fixture/def_foo_mean 656586 ns 656555 ns 10 | |
Fixture/def_foo_median 653640 ns 653617 ns 10 | |
Fixture/def_foo_stddev 10342 ns 10317 ns 10 | |
Fixture/def__eq__ 683131 ns 683110 ns 8325 | |
Fixture/def__eq__ 693190 ns 693120 ns 8325 | |
Fixture/def__eq__ 682600 ns 682576 ns 8325 | |
Fixture/def__eq__ 682688 ns 682665 ns 8325 | |
Fixture/def__eq__ 682953 ns 682931 ns 8325 | |
Fixture/def__eq__ 683107 ns 683086 ns 8325 | |
Fixture/def__eq__ 681995 ns 681974 ns 8325 | |
Fixture/def__eq__ 684419 ns 684396 ns 8325 | |
Fixture/def__eq__ 686120 ns 686094 ns 8325 | |
Fixture/def__eq__ 681725 ns 681703 ns 8325 | |
Fixture/def__eq___mean 684193 ns 684165 ns 10 | |
Fixture/def__eq___median 683030 ns 683008 ns 10 | |
Fixture/def__eq___stddev 3404 ns 3390 ns 10 | |
Fixture/def_foo_with_hash_fix 699457 ns 699431 ns 7632 | |
Fixture/def_foo_with_hash_fix 706079 ns 706053 ns 7632 | |
Fixture/def_foo_with_hash_fix 705114 ns 705088 ns 7632 | |
Fixture/def_foo_with_hash_fix 706765 ns 706739 ns 7632 | |
Fixture/def_foo_with_hash_fix 710803 ns 710782 ns 7632 | |
Fixture/def_foo_with_hash_fix 705319 ns 705297 ns 7632 | |
Fixture/def_foo_with_hash_fix 706966 ns 706943 ns 7632 | |
Fixture/def_foo_with_hash_fix 706887 ns 706860 ns 7632 | |
Fixture/def_foo_with_hash_fix 707344 ns 707321 ns 7632 | |
Fixture/def_foo_with_hash_fix 709082 ns 709058 ns 7632 | |
Fixture/def_foo_with_hash_fix_mean 706382 ns 706357 ns 10 | |
Fixture/def_foo_with_hash_fix_median 706826 ns 706800 ns 10 | |
Fixture/def_foo_with_hash_fix_stddev 2968 ns 2969 ns 10 | |
Fixture/def__eq__with_hash_fix 661174 ns 661153 ns 8013 | |
Fixture/def__eq__with_hash_fix 661124 ns 661105 ns 8013 | |
Fixture/def__eq__with_hash_fix 661409 ns 661383 ns 8013 | |
Fixture/def__eq__with_hash_fix 660521 ns 660497 ns 8013 | |
Fixture/def__eq__with_hash_fix 662285 ns 662264 ns 8013 | |
Fixture/def__eq__with_hash_fix 662994 ns 662972 ns 8013 | |
Fixture/def__eq__with_hash_fix 663534 ns 663512 ns 8013 | |
Fixture/def__eq__with_hash_fix 663536 ns 663512 ns 8013 | |
Fixture/def__eq__with_hash_fix 662686 ns 662660 ns 8013 | |
Fixture/def__eq__with_hash_fix 661735 ns 661712 ns 8013 | |
Fixture/def__eq__with_hash_fix_mean 662100 ns 662077 ns 10 | |
Fixture/def__eq__with_hash_fix_median 662010 ns 661988 ns 10 | |
Fixture/def__eq__with_hash_fix_stddev 1065 ns 1065 ns 10 | |
# # # # # Third run # # # # # # | |
2020-07-10T23:39:12+03:00 | |
Running ./benchmark_def | |
Run on (4 X 3600 MHz CPU s) | |
CPU Caches: | |
L1 Data 32 KiB (x4) | |
L1 Instruction 32 KiB (x4) | |
L2 Unified 256 KiB (x4) | |
L3 Unified 6144 KiB (x1) | |
Load Average: 0.95, 0.97, 0.97 | |
-------------------------------------------------------------------------------- | |
Benchmark Time CPU Iterations | |
-------------------------------------------------------------------------------- | |
Fixture/def_foo 691913 ns 690410 ns 6708 | |
Fixture/def_foo 647460 ns 646354 ns 6708 | |
Fixture/def_foo 647714 ns 646400 ns 6708 | |
Fixture/def_foo 653095 ns 651498 ns 6708 | |
Fixture/def_foo 648493 ns 647450 ns 6708 | |
Fixture/def_foo 656398 ns 653653 ns 6708 | |
Fixture/def_foo 655499 ns 652475 ns 6708 | |
Fixture/def_foo 650337 ns 649173 ns 6708 | |
Fixture/def_foo 649830 ns 648431 ns 6708 | |
Fixture/def_foo 649177 ns 648057 ns 6708 | |
Fixture/def_foo_mean 654992 ns 653390 ns 10 | |
Fixture/def_foo_median 650084 ns 648802 ns 10 | |
Fixture/def_foo_stddev 13346 ns 13247 ns 10 | |
Fixture/def__eq__ 692092 ns 690736 ns 8457 | |
Fixture/def__eq__ 689879 ns 688581 ns 8457 | |
Fixture/def__eq__ 690967 ns 689556 ns 8457 | |
Fixture/def__eq__ 691490 ns 690281 ns 8457 | |
Fixture/def__eq__ 693354 ns 692074 ns 8457 | |
Fixture/def__eq__ 696450 ns 695137 ns 8457 | |
Fixture/def__eq__ 692164 ns 690763 ns 8457 | |
Fixture/def__eq__ 692217 ns 690955 ns 8457 | |
Fixture/def__eq__ 693852 ns 692535 ns 8457 | |
Fixture/def__eq__ 693955 ns 692558 ns 8457 | |
Fixture/def__eq___mean 692642 ns 691318 ns 10 | |
Fixture/def__eq___median 692190 ns 690859 ns 10 | |
Fixture/def__eq___stddev 1846 ns 1845 ns 10 | |
Fixture/def_foo_with_hash_fix 702953 ns 701494 ns 7156 | |
Fixture/def_foo_with_hash_fix 703803 ns 702511 ns 7156 | |
Fixture/def_foo_with_hash_fix 706672 ns 705263 ns 7156 | |
Fixture/def_foo_with_hash_fix 706497 ns 705176 ns 7156 | |
Fixture/def_foo_with_hash_fix 708612 ns 707334 ns 7156 | |
Fixture/def_foo_with_hash_fix 709017 ns 707553 ns 7156 | |
Fixture/def_foo_with_hash_fix 710421 ns 709102 ns 7156 | |
Fixture/def_foo_with_hash_fix 709396 ns 707993 ns 7156 | |
Fixture/def_foo_with_hash_fix 709849 ns 708223 ns 7156 | |
Fixture/def_foo_with_hash_fix 708423 ns 707059 ns 7156 | |
Fixture/def_foo_with_hash_fix_mean 707564 ns 706171 ns 10 | |
Fixture/def_foo_with_hash_fix_median 708518 ns 707197 ns 10 | |
Fixture/def_foo_with_hash_fix_stddev 2539 ns 2522 ns 10 | |
Fixture/def__eq__with_hash_fix 669580 ns 668274 ns 8129 | |
Fixture/def__eq__with_hash_fix 669837 ns 668389 ns 8129 | |
Fixture/def__eq__with_hash_fix 670823 ns 669610 ns 8129 | |
Fixture/def__eq__with_hash_fix 672062 ns 670605 ns 8129 | |
Fixture/def__eq__with_hash_fix 670879 ns 669545 ns 8129 | |
Fixture/def__eq__with_hash_fix 672509 ns 671292 ns 8129 | |
Fixture/def__eq__with_hash_fix 670823 ns 669471 ns 8129 | |
Fixture/def__eq__with_hash_fix 671237 ns 669948 ns 8129 | |
Fixture/def__eq__with_hash_fix 672271 ns 670826 ns 8129 | |
Fixture/def__eq__with_hash_fix 671823 ns 670502 ns 8129 | |
Fixture/def__eq__with_hash_fix_mean 671184 ns 669846 ns 10 | |
Fixture/def__eq__with_hash_fix_median 671058 ns 669779 ns 10 | |
Fixture/def__eq__with_hash_fix_stddev 992 ns 997 ns 10 | |
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 <benchmark/benchmark.h> | |
#include <pybind11/embed.h> | |
namespace py = pybind11; | |
struct Foo { | |
void foo(const Foo &, const Foo &) {} | |
}; | |
bool operator==(const Foo &, const Foo &) { | |
return true; | |
} | |
void hash_fix_overhead(py::class_<Foo> &foo, const char *name_) { | |
if (strcmp(name_, "__eq__") == 0 && !foo.attr("__dict__").contains("__hash__")) { | |
foo.attr("__hash__") = py::none(); | |
} | |
} | |
PYBIND11_EMBEDDED_MODULE(embeded_module, m) { | |
py::class_<Foo>(m, "Foo"); | |
} | |
class Fixture : public benchmark::Fixture { | |
public: | |
void SetUp(const ::benchmark::State &state) override { | |
guard = std::make_unique<py::scoped_interpreter>(); | |
m = std::make_unique<py::module>(py::module::import("embeded_module")); | |
} | |
void TearDown(const ::benchmark::State &state) override { | |
m.reset(); | |
guard.reset(); | |
} | |
std::unique_ptr<py::module> m; | |
private: | |
std::unique_ptr<py::scoped_interpreter> guard; | |
}; | |
BENCHMARK_F(Fixture, def_foo)(benchmark::State &st) { | |
{ | |
py::class_<Foo> foo(m->attr("Foo")); | |
for (auto _ : st) { | |
foo.def("foo", &Foo::foo); | |
} | |
} | |
} | |
BENCHMARK_F(Fixture, def_foo_with_hash_fix)(benchmark::State &st) { | |
{ | |
py::class_<Foo> foo(m->attr("Foo")); | |
for (auto _ : st) { | |
foo.def_hash_fix("foo", &Foo::foo); | |
} | |
} | |
} | |
BENCHMARK_F(Fixture, def__eq__)(benchmark::State &st) { | |
{ | |
py::class_<Foo> foo(m->attr("Foo")); | |
for (auto _ : st) { | |
foo.def("__eq__", py::overload_cast<const Foo &, const Foo &>(&::operator==)); | |
} | |
} | |
} | |
BENCHMARK_F(Fixture, def__eq__with_hash_fix)(benchmark::State &st) { | |
{ | |
py::class_<Foo> foo(m->attr("Foo")); | |
for (auto _ : st) { | |
foo.def_hash_fix("__eq__", py::overload_cast<const Foo &, const Foo &>(&::operator==)); | |
} | |
} | |
} | |
BENCHMARK_F(Fixture, hash_fix_overhead)(benchmark::State &st) { | |
{ | |
py::class_<Foo> foo(m->attr("Foo")); | |
foo.def("__eq__", py::overload_cast<const Foo &, const Foo &>(&::operator==)); | |
for (auto _ : st) { | |
hash_fix_overhead(foo, "__eq__"); | |
} | |
} | |
} | |
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
// line 1150 | |
template <typename Func, typename... Extra> | |
class_ &def_hash_fix(const char *name_, Func&& f, const Extra&... extra) { | |
cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this), | |
sibling(getattr(*this, name_, none())), extra...); | |
attr(cf.name()) = cf; | |
if (strcmp(name_, "__eq__") == 0 && !attr("__dict__").contains("__hash__")) { | |
attr("__hash__") = none(); | |
} | |
return *this; | |
} | |
// line 1161 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment