Deutsch English Français Italiano |
<varss8$de8j$1@raubtier-asyl.eternal-september.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail From: Bonita Montero <Bonita.Montero@gmail.com> Newsgroups: comp.lang.c Subject: Re: Top 10 most common hard skills listed on resumes... Date: Fri, 30 Aug 2024 09:37:47 +0200 Organization: A noiseless patient Spider Lines: 79 Message-ID: <varss8$de8j$1@raubtier-asyl.eternal-september.org> References: <vab101$3er$1@reader1.panix.com> <vad8lr$1fv5u$1@dont-email.me> <vaf7f0$k51$2@reader1.panix.com> <vafgb2$1to4v$2@dont-email.me> <vafkdk$1ut4h$2@raubtier-asyl.eternal-september.org> <20240825192810.0000672c@yahoo.com> <vafs6u$21ofd$1@raubtier-asyl.eternal-september.org> <vafsst$20j4p$3@dont-email.me> <vaj3c4$2lb2c$1@dont-email.me> <vaj46o$2kusd$2@dont-email.me> <vajvoh$2t849$1@dont-email.me> <vak35f$2tphj$1@raubtier-asyl.eternal-september.org> <vak6f8$2tsqj$2@dont-email.me> <vak7c0$2ufit$1@raubtier-asyl.eternal-september.org> <vaki4u$303sg$1@dont-email.me> <vakjff$30c4f$1@raubtier-asyl.eternal-september.org> <val7d6$33e83$1@dont-email.me> <vamb0t$3btll$2@raubtier-asyl.eternal-september.org> <vamqfc$3e42u$1@dont-email.me> <vamrra$3e9lv$1@raubtier-asyl.eternal-september.org> <20240828134323.00003ce1@yahoo.com> <van039$3es48$1@raubtier-asyl.eternal-september.org> <20240828150632.000032aa@yahoo.com> <van5sl$3fnkh$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 30 Aug 2024 09:37:44 +0200 (CEST) Injection-Info: raubtier-asyl.eternal-september.org; posting-host="22fa54b4466af970c3e8d7b644a2159b"; logging-data="440595"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8Hr+iHQvFOnax750n81ZaMe88T/7zFeA=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:E9DvTO0wGr6Jr/MrYMSKgGnqYAk= Content-Language: de-DE In-Reply-To: <van5sl$3fnkh$1@raubtier-asyl.eternal-september.org> Bytes: 4522 I've run the numbers on different kinds of virtual indirection: #include <iostream> #include <functional> #include <chrono> #include <atomic> #include <memory> using namespace std; using namespace chrono; static atomic_int x( 0 ); auto adder = []( int a, int b ) { return a + b + ::x.load( memory_order_relaxed ); }; struct V { virtual int operator ()( int a, int b ) { return adder( a, b ); } }; function<int ( int, int )> fn( adder ); function<int ( int, int )> fnRef( ref( adder ) ); function<int ( int, int )> fnC( +adder ); atomic<int (*)( int, int )> cFn( +adder ); unique_ptr<V> v( make_unique<V>() ); int main() { int sum = 0; auto bench = [&]<typename Fn>( char const *what, Fn fn ) { using hr_t = high_resolution_clock; using dur_t = hr_t::duration; dur_t tMin = dur_t::max(); for( int turn = 100; turn--; ) { auto start = hr_t::now(); for( int a = 100; a--; ) for( int b = 100; b--; ) sum += fn( a, b ); dur_t dur = hr_t::now() - start; tMin = tMin > dur ? dur : tMin; } cout << what << duration_cast<nanoseconds>( tMin ).count() / 1.0e4 << endl; }; bench( "function<>: ", []( int a, int b ) { return ::fn( a, b ); } ); bench( "function<> ref'd: ", []( int a, int b ) { return ::fnRef( a, b ); } ); bench( "function<> C'd: ", []( int a, int b ) { return ::fnC( a, b ); } ); bench( "virtual method: ", []( int a, int b ) { return (*::v)( a, b ); } ); bench( "c-function ptr: ", []( int a, int b ) { return ::cFn.load( memory_order_relaxed )( a, b ); } ); bench( "compile-time polymorphism: ", ::adder ); return sum; } This are the results for my Zen4-CPU: function<>: 0.91 function<> ref'd: 1.09 function<> C'd: 1.1 virtual method: 0.92 c-function ptr: 0.95 compile-time polymorphism: 0.41 So there's only half a nanosecond more overhead when using a function<> -object with external storage (similar to a virtual function, but only with one indirection since there's no virtual function table) over di- rect inlining. No matter if you have a function<>-object, whether it is initialized with a reference_wrapper (no external storage) or a C func- tion pointer or if you use a C function pointler directly: it's all the same performance. Although there are different complexities there are enough calculation units in the Zen4-CPU to do it all in about the same time.