I would be grateful for help on correcting or confirming

my understanding of the second call to getrusage function

in speller.c.

The first call assigns certain values to both before and

after variables by calling load(dictionary) function.

Does the second call to getrusage within the for loop

in the main function overwrites previous values of

before and after and now new values are assigned

based on the call to check(word) function? But we

still have the time computed after the first call

because this time had been assigned to the variable

time_load, correct?

1st call:

// load dictionary
    getrusage(RUSAGE_SELF, &before);
    bool loaded = load(dictionary);
    getrusage(RUSAGE_SELF, &after);

2nd call:

// check word's spelling
            getrusage(RUSAGE_SELF, &before);
            bool misspelled = !check(word);
            getrusage(RUSAGE_SELF, &after);

Thank you!


In case you haven't figured it out, the speller program collects computer time usage data on each of the four functions of load(), check(), size() and unload(), using the getrusage() function. In simple terms, it gets timestamps before and after each of the 4 target functions and calculates time used.

The calls to getrusage(...) are strictly to calculate the computing time expended between the &before and &after calls. The activities of the functions between the two calls have no direct bearing on the data generated by getrusage().

For the load() function, it's a single call to determine total processing time used by the load() function, so getrusage() only needs to be called once before and once after. The data in those two structures is then compared to get the processing time.

For check, it's a little different. This is in a for loop because the speller code itself has to get a word from the text file under test and check that it's a valid word to be submitted to the check() function (there are a number that are not valid). After all that other stuff is done, the check() function will be called for that word. It's important that all that housekeeping is done outside of the timing mechanism of getrusage() so that it doesn't impact the collection of timing data.

On each pass through the for loop, only one word is submitted to the check() function. Because of this, the code was set up to accumulate the time data. You are correct that each pass overwrites the data in &before and &after. This is not a problem though, because the elapsed time data is calculated and a running total is kept in time_check, so the data is not lost. The two var structures are just being reused on each pass.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .