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

  • SEARCH
  • Home
  • 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 9014923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:37:37+00:00 2026-06-16T03:37:37+00:00

Now I am using valgrind/callgrind to meaure and compare different algorithm implementations. Suppose there

  • 0

Now I am using valgrind/callgrind to meaure and compare different algorithm implementations. Suppose there are two implementations for an algorithm, and the processing chains are as follows:

void main()
{
    //Procedure 1
    fun1();
    //Procedure 2
    fun2();
    //Procedure 3
    Algorithm_imp_1();  //Algorithm_imp_2(); 
   //Procedure 4
    fun4();
}

The difference between these two implementations lies in the third procedure, where two different algorithm implementations are performed. In order to see which implementation is better, I am now using valgrind/callgrind with the help of kachegrind.

As far as I can image, if Algorithm_imp_1() is more efficient than Algorithm_imp_2(), its two indicators: one is the absolute time that have been used for running the program , and the other is the percentage of the time the third procedure takes, should be smaller. However, what I have obtained with valgrind are very confusing:

Method 1 whole procedure: overall circle 106858896849   
        Algorithm_imp_1():  circle 2971828457(3.03%)
Method 2 whole procedure: overall circle 137889090577
        Algorithm_imp_2(): circle 351826053(0.26%)

Since the whole procedure for the both methods are the same expect the third part, if the consumed time percentage of the third part is small, we can expect the overall time for running the program should also be small. However, what we observed above are contradictory. I was wondering what’s wrong with my analysis. 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-06-16T03:37:38+00:00Added an answer on June 16, 2026 at 3:37 am

    The shell has a built-in time command. If you use that, it will display wall clock as well as user and system times.

    linux> /bin/sh
    $ time a.out
        0m1.01s real     0m0.00s user     0m0.00s system
    

    If you need a better resolution than 0.01 second, you can use getrusage and gettimeofday.

    When profiling a function, it is best to place instrumentation as near the function as possible. Using something like valgrind will most likely mask what is going on within the function.

    The signal to noise ratio of the measurement has to be high for it to be useful.

    linux> cat a.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    /*
     * Time elapsed in milli-seconds. Tweak this to get micro-seconds. 
     */
    
    int
    time_diff(struct timeval *t2, struct timeval *t1)
    {
        int r;
    
        r = (t2->tv_sec - t1->tv_sec)*1000 + t2->tv_usec/1000 - t1->tv_usec/1000;
    
        return(r);
    }
    
    int
    main()
    {
        struct rusage r1, r2;
        struct timeval t1, t2;
    
        (void)gettimeofday(&t1, 0);
        (void)getrusage(RUSAGE_SELF, &r1);
    
        sleep(1); /* Function to be profiled. */
    
        (void)getrusage(RUSAGE_SELF, &r2);
        (void)gettimeofday(&t2, 0);
    
        printf("real = %d (ms), user = %d (ms), system = %d (ms)\n", 
                time_diff(&t2, &t1),
                time_diff(&r2.ru_utime, &r1.ru_utime),
                time_diff(&r2.ru_stime, &r1.ru_stime));
    
        return(0);
    }
    

    When you run the above, you should see something similar to below.

    linux> a.out
    real = 1000 (ms), user = 0 (ms), system = 0 (ms)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

t = Time.now d = 10.minutes.from_now Using these two variables, how do I output
Is there any recommended Java application profiling tutorial? I am now using JProfiler and
im now using namespaces in PHP 5.3 now there is a fallback mechanism for
We are now using Django to develop a multilingual website. We have different language
I am now using asp.net mvc and wondering what is a better choice using
I am now using Emacs 23 with visual-line-mode turned of for text editing but
We are using a Java EE application and we are right now using Informix
I am from PHP and now using Django, I like match against queries of
The script below worked on my Mac OS X. I'm now using Ubuntu OS,
I just started practicing TDD in my projects. I'm developing a project now using

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.