Rate counter

From Rosetta Code
Revision as of 04:03, 6 December 2009 by Tikkanz (talk | contribs) (→‎{{header|J}}: whitespace)
Task
Rate counter
You are encouraged to solve this task according to the task description, using any language you may know.

Counting the frequency at which something occurs is a common activity in measuring performance and managing resources. In this task, we assume that there is some job which we want to perform repeatedly, and we want to know how quickly these jobs are being performed.

Of interest is the code that performs the actual measurements. Any other code (such as job implementation or dispatching) that is required to demonstrate the rate tracking is helpful, but not the focus.

Multiple approaches are allowed (even preferable), so long as they can accomplish these goals:

  • Run N seconds worth of jobs and/or Y jobs.
  • Report at least three distinct times.

Be aware of the precision and accuracy limitations of your timing mechanisms, and document them if you can.

C

This code stores all of the data of the rate counter and its configuration in an instance of a struct named rate_state_s, and a function named tic_rate is called on that struct instance every time we complete a job. If a configured time has elapsed, tic_rate calculates and reports the tic rate, and resets the counter.

<lang c>#include <stdio.h>

  1. include <time.h>

// We only get one-second precision on most systems, as // time_t only holds seconds. struct rate_state_s {

   time_t lastFlush;
   time_t period;
   size_t tickCount;

};

void tic_rate(struct rate_state_s* pRate) {

   pRate->tickCount += 1;
   time_t now = time(NULL);
   if((now - pRate->lastFlush) >= pRate->period)
   {
       //TPS Report
       size_t tps = 0.0;
       if(pRate->tickCount > 0)
           tps = pRate->tickCount / (now - pRate->lastFlush);
       printf("%u tics per second.\n", tps);
       //Reset
       pRate->tickCount = 0;
       pRate->lastFlush = now;
   }

}

// A stub function that simply represents whatever it is // that we want to multiple times. void something_we_do() {

   // We use volatile here, as many compilers will optimize away
   // the for() loop otherwise, even without optimizations
   // explicitly enabled.
   //
   // volatile tells the compiler not to make any assumptions
   // about the variable, implying that the programmer knows more
   // about that variable than the compiler, in this case.
   volatile size_t anchor = 0;
   size_t x = 0;
   for(x = 0; x < 0xffff; ++x)
   {
       anchor = x;
   }

}

int main() {

   time_t start = time(NULL);
   struct rate_state_s rateWatch;
   rateWatch.lastFlush = start;
   rateWatch.tickCount = 0;
   rateWatch.period = 5; // Report every five seconds.
   time_t latest = start;
   // Loop for twenty seconds
   for(latest = start; (latest - start) < 20; latest = time(NULL))
   {
       // Do something.
       something_we_do();
       // Note that we did something.
       tic_rate(&rateWatch);
   }
   return 0;

} </lang>

C++

This code defines the counter as a class, CRateState. The counter's period is configured as an argument to its constructor, and the rest of the counter state is kept as class members. A member function Tick() manages updating the counter state, and reports the tic rate if the configured period has elapsed.

<lang cpp>#include <iostream>

  1. include <time.h>

// We only get one-second precision on most systems, as // time_t only holds seconds. class CRateState { protected:

   time_t m_lastFlush;
   time_t m_period;
   size_t m_tickCount;

public:

   CRateState(time_t period);
   void Tick();

};

CRateState::CRateState(time_t period) {

   m_lastFlush = time(NULL);
   m_period = period;
   m_tickCount = 0;

}

void CRateState::Tick() {

   m_tickCount += 1;
   time_t now = time(NULL);
   if((now - m_lastFlush) >= m_period)
   {
       //TPS Report
       size_t tps = 0.0;
       if(m_tickCount > 0)
           tps = m_tickCount / (now - m_lastFlush);
       std::cout << tps << " tics per second" << std::endl;
       //Reset
       m_tickCount = 0;
       m_lastFlush = now;
   }

}

// A stub function that simply represents whatever it is // that we want to multiple times. void something_we_do() {

   // We use volatile here, as many compilers will optimize away
   // the for() loop otherwise, even without optimizations
   // explicitly enabled.
   //
   // volatile tells the compiler not to make any assumptions
   // about the variable, implying that the programmer knows more
   // about that variable than the compiler, in this case.
   volatile size_t anchor = 0;
   for(size_t x = 0; x < 0xffff; ++x)
   {
       anchor = x;
   }

}

int main() {

   time_t start = time(NULL);
   CRateState rateWatch(5);
   time_t latest = start;
   // Loop for twenty seconds
   for(latest = start; (latest - start) < 20; latest = time(NULL))
   {
       // Do something.
       something_we_do();
       // Note that we did something.
       rateWatch.Tick();
   }
   return 0;

} </lang>

J

Solution
<lang j> x (6!:2) y</lang> The foreign conjunction 6!:2 will execute the code y (right argument), x times (left argument) and report the average time in seconds required for one execution.

Example: <lang j> list=: 1e6 ?@$ 100 NB. 1 million random integers from 0 to 99

  freqtable=: ~. ,. #/.~       NB. verb to calculate and build frequency table
  20 (6!:2) 'freqtable list'   NB. calculate and build frequency table for list, 20 times

0.00994106</lang>

UNIX Shell

This code stores the number of times the program task can complete in 20 seconds. It is two parts.

Part 1: file "foo.sh"

This script spins, executing task as many times as possible. <lang bash>

  1. !/bin/bash

while : ; do task && echo >> .fc done </lang>

Part 2: This script runs foo.sh in the background, and checks the rate count file every five seconds. After four such checks, twenty seconds will have elapsed. <lang bash> ./foo.sh & sleep 5 mv .fc .fc2 2>/dev/null wc -l .fc2 2>/dev/null rm .fc2 sleep 5 mv .fc .fc2 2>/dev/null wc -l .fc2 2>/dev/null sleep 5 mv .fc .fc2 2>/dev/null wc -l .fc2 2>/dev/null sleep 5 killall foo.sh wc -l .fc 2>/dev/null rm .fc </lang>