Divine Blood
You are not logged in.


Source Code: http://www.divineblood.org/snippets/timer.c

Last Modified: Fri May 21, 2004 6:46pm
Download without markup
Download as zipfile


/*
 * This is a command for the game that shows execution time for
 * a command.  I wrote it to compare execution time of various
 * track snippets against my own.  But you may use it to test
 * other possibly resource hungry commands.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "merc.h"

void do_timer(CHAR_DATA *ch, char *argument)
{
  struct rusage pre_usage;
  struct rusage post_usage;
  struct timeval pre_time;
  struct timeval post_time;
  double usr, sys, real;

  char buf[1024];

  gettimeofday(&pre_time, 0);
  getrusage(RUSAGE_SELF, &pre_usage);
  interpret(ch, argument);
  getrusage(RUSAGE_SELF, &post_usage);
  gettimeofday(&post_time, 0);
  usr = (post_usage.ru_utime.tv_sec + post_usage.ru_utime.tv_usec/1000000.0)
    - (pre_usage.ru_utime.tv_sec + pre_usage.ru_utime.tv_usec/1000000.0);
  sys = (post_usage.ru_stime.tv_sec + post_usage.ru_stime.tv_usec/1000000.0)
    - (pre_usage.ru_stime.tv_sec + pre_usage.ru_stime.tv_usec/1000000.0);
  real = (post_time.tv_sec + post_time.tv_usec/1000000.0)
    - (pre_time.tv_sec + pre_time.tv_usec/1000000.0);
  sprintf(buf, "Usr time:  %0.05f\n\r", usr);
  send_to_char(buf, ch);
  sprintf(buf, "Sys time:  %0.05f\n\r", sys);
  send_to_char(buf, ch);
  sprintf(buf, "Real time: %0.05f\n\r", real);
  send_to_char(buf, ch);
  sprintf(buf, "Page faults: %ld\n\r", post_usage.ru_majflt - pre_usage.ru_majflt);
  send_to_char(buf, ch);
  sprintf(buf, "Page reclaims: %ld\n\r", post_usage.ru_minflt - pre_usage.ru_minflt);
  send_to_char(buf, ch);
  sprintf(buf, "Swaps: %ld\n\r", post_usage.ru_nswap - pre_usage.ru_nswap);
  send_to_char(buf, ch);
}

© 2005 Divine Blood Staff -- Web page by Palrich (palrichatgmail.com)