Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:04:00+00:00 2026-05-13T12:04:00+00:00

I would like to use scope guard in C in order to do profiling.

  • 0

I would like to use scope guard in C in order to do profiling.

I would like to know how much time I spend in a function. Here is what I do:

int function() {

  tic();

  ... do stuff ...
  if (something)
  {
    toc();
    return 0;
   }

  toc();
  return 1;
}

I need to place a toc statement each time I exit the function. I would like to do that without having to copy paste toc everywhere. Is there a generic way to do that, using a macro or something ?
Also I don’t want to change the way the function is called, as there are many functions I have to profile.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:04:00+00:00Added an answer on May 13, 2026 at 12:04 pm

    This doesn’t change the way the function is called. Probably not much use if you want to be able to profile every single function, though.

    static inline int real_function() {
        // previous contents of function(), with no tic or toc
    }
    
    int function() {
        tic();
        int r = real_function();
        toc();
        return r;
    }
    

    As everyone else says: use a profiler, it will save you a lot of effort in the long run. As they don’t say: if your platform has one.

    If it doesn’t, then the easiest might be to say (as a coding rule) that functions must have only one exit point, and that exit point must be via your macro. Then you can manually instrument all your functions with code at entry and exit. Legacy functions with multiple returns can be wrapped up as above.

    Also, bear in mind when you’re doing anything like this that your compiler can mess you up. You might write this:

    tic();
    do_something();
    int i = something_else();
    toc();
    return i;
    

    If the compiler determines that something_else has no side-effects, then even though something_else takes significant time, it might turn the code into this:

    tic();
    do_something();
    toc();
    return something_else();
    

    And your profile data will under-estimate the time spent in your function. Another reason it’s so good to have a real profiler – it can co-operate with the compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use the logout function from Django but not sure how
I would like to pass the variable from a function to the main scope
I would like to bind a variable to a function's scope, I can do
I would like to use a boost::shared_ptr in order for WSACleanup() to be called
According to my question here I would like to use SCHED_RR with pthread_setschedparam for
I would like to know if there is a way to use i18n with
I would like to make use of request scoped beans in my app. I
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.