Thread: Large function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Large function

    In general, does spliting a large function in smallers one increase its speed?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I don't know if splitting it up would increase speed, but I do know that making a function do too many things defeats the whole purpose of using a function in the first place. A rule of thumb is this: A function should ideally do one thing, and do it well.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In general, all other factors being equal -- no. There is a very small slice of time needed, for any call to another function, to set up the stack.

    With multi-layered cache's, and modern compilers however, you can be certain that all other factors are NOT equal.

    If you want to optimize your program:

    1) Find the least big O algorithm that clearly is accurate. Avoid optimizing yet.

    2) Use a profiler on typical input, and see where any bottlenecks are, in the run time.

    3) Optimize your bottlenecked sections of code.

    4) And re-test with the profiler.

    Test first, test last, and avoid making the code obscenely obscure or "cute". In general, it's a bad idea to generalize - you want tests, not hand-waving and smoke blowing.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In general, does spliting a large function in smallers one increase its speed?
    It increases the speed you can write, test, debug and maintain it.

    If you can't see the whole function on screen at the same time, then you should consider splitting it up.

    There are obvious exceptions, say a large switch/case in a top-level message decoder splitting the work into specific functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Kempelen View Post
    In general, does spliting a large function in smallers one increase its speed?
    Splitting a function up arbitrarily doesn't help anything.

    Splitting a function up into specific sub-tasks helps productivity, but doesn't really affect execution speed.

    Finding an operation, even a small one, within the large function, that is performed more than once and factoring that out into a separate function, can improve execution speed as well as productivity.

    All "in general" of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM