Results 1 to 4 of 4

Thread: "Cost" of calling a function

  1. #1
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,420

    "Cost" of calling a function

    I am curious what the physical "cost" is of calling a function. What really happens to memory when that function is called.

    Let's say I've got a complicated loop running in code - and I want to take the "contents" of that loop logic and put it in another C++ function.

    If the "variables" and what-not (I've got large arrays and "strings" passed from "managed" code here as well) are all passed by REFERENCE - so no memory is copied for that what is other costs involved?

    Is there a GOSUB-like or JSB-like statement in C++ that could help me visually organize this code without incurring the cost of calling functions??
    Last edited by szlamany; Sep 1st, 2012 at 11:07 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,415

    Re: "Cost" of calling a function

    Moved to C++ Forum from C++ Codebank.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: "Cost" of calling a function

    You might want to have a look at this and perhaps investigate further along that line.

    http://www.codeproject.com/Articles/...with-the-stack

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,420

    Re: "Cost" of calling a function

    Thank you - that was a great read.

    I'm doing loops on 500,000+ byte chunks of memory representing the text of documents. Each character is passed through lots of conditions and also looked at against arrays of rules.

    It's that moment of looking at the arrays of rules that I was thinking of removing to a separate function body - passing in whatever is needed by reference. I can see from that article how the stack builds up when that happens.

    I was concerned because I was looking at it from the "outside" - where the big loop on the memory chunk is being done. But actually the amount of code running against the array of rules is huge and adding on some stack-framing requirements is most likely inconsequential to the run time.

    Bottom line is I can benchmark and time this myself - and I probably should.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •