Thread: Help with macros

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Question Help with macros

    We are trying to perform performance calculation (CPU cycles taken) on our functions in code.
    We want to do this with minimal changes in code.

    So if we have somewhere:
    print_hi("hello world");

    We shall replace it with:
    CALL(print_hi("hello world"));

    where: CALL expands to:
    #define CALL(a) get_ticks( ); a; get_ticks_and_compare()

    // Where get_ticks( ), get_ticks_and_compare( ) implement the logic of calculating start and end cycles.

    Now, the function being encapsulated may have return values. Say:
    result=CALL(add(2,3));

    So we change our CALL macro to:
    #define CALL(a) (get_ticks( ), a); get_ticks_and_compare();

    But we could also have 'a' getting called from a for-loop statement:
    for (i = 0; i < add(b, c); i++)

    In such a case, we cannot use ';' in the CALL expansion as it would invalidate the for statement.

    Please suggest any good ways of writing the CALL( ) macro that can solve our purpose.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You might be able to do this
    #define CALL(a) get_ticks( ), a, get_ticks_and_compare()

    Along with this

    CALL(result=add(2,3));

    and
    for (i = 0; i < CALL(add(b, c)); i++)

    But seriously, get a profiler.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What different in macros?
    By dpp in forum C++ Programming
    Replies: 8
    Last Post: 12-11-2009, 11:35 AM
  2. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  3. Macros
    By jsbeckton in forum C Programming
    Replies: 11
    Last Post: 12-02-2005, 02:10 AM
  4. Macros Using #s
    By Krak in forum C++ Programming
    Replies: 21
    Last Post: 07-18-2005, 01:03 AM
  5. macros
    By rpc2005 in forum C Programming
    Replies: 23
    Last Post: 06-14-2005, 08:56 AM