Thread: How to create a mini-benchmarking program?

  1. #1
    Registered User
    Join Date
    Sep 2016

    Question How to create a mini-benchmarking program?

    I have an assignment where I need to make a benchmark program to test the performance of any processor with two sorting algorithms (an iterative one and a recursive one). The thing is my teacher told me I had to create three different programs (that is, 3 .c files), two with each sorting algorithm (both of them have to read integers from a text file separated with \n's and write the same numbers to another text file but sorted), and a benchmarking program. In the benchmark program I need to calculate the MIPs (million instructions per second) with the formula MIPs = NI/T*10^6, where NI is the number of instructions and T is the time required to execute those instructions. I have to be able to estimate the time each algorithm will take on any processor by calculating its MIPs and then solving that equation for T, like EstimatedTime = NI/MIPs*10^6. My question is... how exactly do I measure the execution time of a .c file from another .c file? I have never done something like that. I mean, I guess I can use the TIME functions in C and measure the time to execute X number of lines and stuff, but I can do that only if all 3 functions (2 sorting algorithms and 1 benchmark function) are in the same program. I don't even know how to start.

    Oh and btw, I have to calculate the number of instructions by cross compiling the sorting algorithms from C to MIPS (the asm language) and counting how many instructions were used.

    Any guidelines would be appreciated... I currently have these functions:

    • readfile (to read text files with ints on them)
    • writefile
    • sorting algorithms

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Are all three .c files separate programs, each with a main() ?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2016
    I don't actually know, I assumed they are, otherwise my teacher would have told me.
    What's the practical difference for this? :0

  4. #4
    Registered User
    Join Date
    Jun 2015
    Something like this might work (assuming *nix).
    #define _BSD_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    int main(void) {
        struct timespec ts1, ts2;
        clock_gettime(CLOCK_REALTIME, &ts1);
        pid_t pid = fork();
        if (pid == (pid_t)-1) {
        if (pid == 0) {  // child
            execlp("./aprog", "aprog", "afile", (const char *)NULL);
        // parent
        int status = 0;
        clock_gettime(CLOCK_REALTIME, &ts2);
        if (ts2.tv_nsec < ts1.tv_nsec) {
            ts2.tv_nsec += 1000000000;
        printf("%ld.%09ld\n", (long)(ts2.tv_sec - ts1.tv_sec),
            ts2.tv_nsec - ts1.tv_nsec);
        return 0;
    This is also timing the fork and exec, but I don't see a way around that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mini program that i can't solve (one-dimensional)
    By irmate210 in forum C Programming
    Replies: 2
    Last Post: 01-28-2015, 12:21 PM
  2. a bug in my mini program!
    By Perspolice1993 in forum C Programming
    Replies: 3
    Last Post: 09-28-2011, 09:51 PM
  3. Benchmarking
    By redruby147 in forum C Programming
    Replies: 4
    Last Post: 06-18-2009, 07:44 AM
  4. benchmarking?
    By cs32 in forum C Programming
    Replies: 5
    Last Post: 02-14-2008, 07:37 AM
  5. Benchmarking
    By RoD in forum Tech Board
    Replies: 5
    Last Post: 10-14-2002, 02:04 PM

Tags for this Thread