• C-function as fast as std::function<>

    From Bonita Montero@3:633/10 to All on Thu Jun 25 08:19:33 2026
    Run the numbers with this benchmark and give me the timings:

    #include <iostream>
    #include <functional>
    #include <chrono>
    #include <concepts>

    using namespace std;
    using namespace chrono;

    static int (*volatile cFunc)( int );
    static function<int ( int )> stdFunc;

    int main()
    {
    auto funcPerf = []<typename Func>( const char *what, Func &func )
    requires requires { { func( 1 ) } -> convertible_to<int>; }
    {
    constexpr size_t N = 1'000'000'000;
    int value = 0;
    auto start = high_resolution_clock::now();
    for( size_t r = N; r; --r )
    value = func( value );
    double ns = (double)duration_cast<nanoseconds>( high_resolution_clock::now() - start ).count() / (double)N;
    cout << what << ns << endl;
    return (int)value;

    };
    ::stdFunc = []( int a ) { return a + 1; };
    int sum = funcPerf( "std::function<>: ", ::stdFunc );
    ::cFunc = +[]( int a ) { return a + 1; };
    sum += funcPerf( "C-function: ", ::cFunc );
    return sum;
    }

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Scott Lurndal@3:633/10 to All on Thu Jun 25 15:39:31 2026
    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Run the numbers with this benchmark and give me the timings:

    Warum?

    Was ist die punkt?

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Thu Jun 25 19:09:50 2026
    Am 25.06.2026 um 17:39 schrieb Scott Lurndal:

    Bonita Montero <Bonita.Montero@gmail.com> writes:

    Run the numbers with this benchmark and give me the timings:

    Warum?
    Was ist die punkt?

    To see that calling a std::function<>-object with state costs
    the same as calling a C-function - at least on my PC.


    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Thu Jun 25 12:41:23 2026
    On 6/25/2026 10:09 AM, Bonita Montero wrote:
    Am 25.06.2026 um 17:39 schrieb Scott Lurndal:

    Bonita Montero <Bonita.Montero@gmail.com> writes:

    Run the numbers with this benchmark and give me the timings:

    Warum?
    Was ist die punkt?

    To see that calling a std::function<>-object with state costs
    the same as calling a C-function - at least on my PC.


    That's interesting to me because I am trying to code up a little IOCP
    server from memory using only small parts of C++ because its so low
    level, region allocations and shit like that. I am almost to the point
    where I need to start calling callbacks to user code.

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Chris M. Thomasson@3:633/10 to All on Thu Jun 25 12:41:43 2026
    On 6/25/2026 12:41 PM, Chris M. Thomasson wrote:
    On 6/25/2026 10:09 AM, Bonita Montero wrote:
    Am 25.06.2026 um 17:39 schrieb Scott Lurndal:

    Bonita Montero <Bonita.Montero@gmail.com> writes:

    Run the numbers with this benchmark and give me the timings:

    Warum?
    Was ist die punkt?

    To see that calling a std::function<>-object with state costs
    the same as calling a C-function - at least on my PC.


    That's interesting to me because I am trying to code up a little IOCP
    server from memory using only small parts of C++ because its so low
    level, region allocations and shit like that. I am almost to the point
    where I need to start calling callbacks to user code.

    Fwiw, my original code is in pure C.

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Tristan Wibberley@3:633/10 to All on Mon Jun 29 16:05:57 2026
    On 25/06/2026 18:09, Bonita Montero wrote:
    Am 25.06.2026 um 17:39 schrieb Scott Lurndal:

    Bonita Montero <Bonita.Montero@gmail.com> writes:

    Run the numbers with this benchmark and give me the timings:

    Warum?
    Was ist die punkt?

    To see that calling a std::function<>-object with state costs
    the same as calling a C-function - at least on my PC.


    I suppose it's a valid thing to compare on occasion, but modern
    compilers do tend to optimise simple bits of additional structure away
    or into existence.

    So one would expect that calling a function with state has the same
    performance as calling a function with state.

    --
    Tristan Wibberley

    The message body is Copyright (C) 2026 Tristan Wibberley except
    citations and quotations noted. All Rights Reserved except that you may,
    of course, cite it academically giving credit to me, distribute it
    verbatim as part of a usenet system or its archives, and use it to
    promote my greatness and general superiority without misrepresentation
    of my opinions other than my opinion of my greatness and general
    superiority which you _may_ misrepresent. You definitely MAY NOT train
    any production AI system with it but you may train experimental AI that
    will only be used for evaluation of the AI methods it implements.

    --- PyGate Linux v1.5.18
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)