0

I made a program that generates a user predefined number of random numbers and then sorts them and it repeats that as many times as the user wants it to repeat. While doing so, the program tracks how long the key functions take.

For that I defined a struct (I guess I could delete the pPrev but let that aside ..) that I'm allocating dynamically and that works like a charm, at least I think so.

struct sTimes {
    struct sTimes *pNext;
    struct sTimes *pPrev;
    time_t tNumGen;
    time_t tSort;
};

and declared / initialized three things corresponding to the average time calculation:

struct sTimes * pTimes = NULL;  // pointer to times
struct sTimes * pTimeStart;     // pointer to start of times
struct sTimes tAverage;         // average times

Before each generation and sorting I'm doing:

pTimes->tNumGen = clock();
pTimes->tSort = clock();

and after each generation and sort:

pTimes->tNumGen = clock() - pTimes->tNumGen;
pTimes->tSort = clock() - pTimes->tSort;

okay, so that's the basic structure. The cycles and sorting etc works. My problem is that the average times don't get calculated super exactly, and I can't figure out why or how to fix that (I'm getting this instead of this).

I calculated the average times like that:

tAverage.tNumGen = pTimeStart->tNumGen;
tAverage.tSort = pTimeStart->tSort;
pTimes = pTimeStart;
if(pTimes->pNext != NULL)
{
    pTimes = pTimes->pNext;
}
while(pTimes != NULL)
{
    tAverage.tNumGen += pTimes->tNumGen;
    tAverage.tNumGen /= 2;
    tAverage.tSort += pTimes->tSort;
    tAverage.tSort /= 2;
    pTimes = pTimes->pNext;
}

I would be so glad if someone has a fancy answer or at least can give me a solution approach to my problem. And I would probably be even happier if someone can explain why this is happening. Have a gread day :)

rphii
  • 209
  • 2
  • 13
  • 5
    `clock()` returns type `clock_t`, not `time_t`. Also research `CLOCKS_PER_SEC` – chux - Reinstate Monica Aug 21 '19 at 20:22
  • 2
    By always dividing by 2 in the loop, you're doing some sort of weighted average, with the most recent value counting for more than the prior values. You can probably simply add the `tNumGen` values and divide by the number of entries in the list — ditto `tSort` values. – Jonathan Leffler Aug 21 '19 at 20:28
  • Probably not your problem, but see [this answer](https://stackoverflow.com/questions/57439219/comparing-execution-time-of-two-functions/57440637#57440637) for some practical advice on timing functions. – Steve Summit Aug 21 '19 at 20:36
  • That divide by 2 thing is a crude type of filter sometimes used to process incoming readings from a device and is out of place here. – Weather Vane Aug 21 '19 at 20:38
  • Use `time()` and `time_t` instead of `clock`. – S.S. Anne Aug 21 '19 at 20:40
  • 1
    @JL2210 `time()` only gives seconds resolution, and doesn't measure processor time. But Visual C is non-conforming anyway, with `clock()` measuring wall time too, not processor time. – Weather Vane Aug 21 '19 at 20:43
  • okay, thats some nice comments, thanks! and now me as well sees that I did the calculation weird... oops – rphii Aug 21 '19 at 21:08
  • I recommend you delete the question. – Jonathan Leffler Aug 22 '19 at 05:15
  • And a little hint for your next questions: Please invest some seconds to learn how to copy contents out of shell windows and spreadsheets as text. It will make helping so much easier if we don't have to type what is in a screenshot. – the busybee Aug 22 '19 at 06:30

0 Answers0