int main(int argc, char **argv) {

	int i;
	char filename[BUFSIZ];
	char string[BUFSIZ],insn_string[BUFSIZ];
	FILE *fff;
	long long instructions=0;
	double time;
	unsigned long long instructions_max=0,instructions_min=0x7fffffffffffffffULL;
	unsigned long long instructions_total=0,instructions_average=0;
	double time_min=10000.0,time_max=0.0,time_total=0,time_average=0;

	for(i=1;i<=RUNS;i++ ){
		sprintf(filename,"out.%d",i);
		fff=fopen(filename,"r");
		if (fff==NULL) {
			fprintf(stderr,"Couldn't open %s\n",filename);
		}
		else {
			fgets(string,BUFSIZ,fff);
			fgets(string,BUFSIZ,fff);
			fgets(string,BUFSIZ,fff);
			fgets(string,BUFSIZ,fff);
			/* instructions */

			sscanf(string,"%s",insn_string);
			instructions=remove_commas(insn_string);

			fgets(string,BUFSIZ,fff);
			fgets(string,BUFSIZ,fff);
			/* time */
			sscanf(string,"%lf",&time);


			printf("%lf\t%lld\n",time,instructions);

			if (time<time_min) time_min=time;
			if (time>time_max) time_max=time;
			time_total+=time;

			if (instructions<instructions_min) instructions_min=instructions;
			if (instructions>instructions_max) instructions_max=instructions;
			instructions_total+=instructions;

			fclose(fff);
		}

	}

	time_average=time_total/RUNS;
	instructions_average=instructions_total/RUNS;

	printf("Average time: %lf (Max %lf Min %lf)\n",
		time_average,time_max,time_min);
	printf("Average instructions: %lld (Max %lld Min %lld)\n",
		instructions_average,instructions_max,instructions_min);

	return 0;
}
Example #2
0
/*
 * Takes a stock symbol and a file with a very particular format
 * as paramters and returns a pointer to a ticker structure. The
 * ticker structure will contain the values associated with the
 * given <symbol>, as specified in the file <file_name>. If the
 * symbol doesn't exist in this file, all fields of the ticker
 * structure (except the symbol field) will be 0. The symbol field
 * will always be a copy of the <symbol> input parameter.
 */
ticker* get_ticker(const char* symbol, const char* file_name)
{
  FILE* f;
  char* buffer;
  char* line;
  ticker* result;
  int n = 1000;
  int endloop = 0;

  if ((f = fopen(file_name, "r")) == 0)
  {
    printf("Couldn't open %s.", file_name);
    abort();
  }

  result = (ticker*) malloc(sizeof(ticker));
  malloc_chars(&result->symbol, strlen(SYMBOL)+1);
  malloc_chars(&buffer, n);

  strcpy(result->symbol, SYMBOL);
  result->volume_of_transactions = 0;
  result->value_of_transactions = 0.0;
  result->trades = 0;
  result->last_price = 0.0;
  result->reference_price = 0.0;
  result->net_change = 0.0;
  result->percent_change = 0.0;

  while (!feof(f) && !endloop)
  {
    getline(&buffer, &n, f);
    line = strstr(buffer, symbol);
    if (line != 0)
    {
      // get the volume of transactions
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%ld", &result->volume_of_transactions);

      // get the value of transactions
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%f", &result->value_of_transactions);

      // get the number of trades
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%d", &result->trades);

      // get the last price
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%f", &result->last_price);

      // get the reference price
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%f", &result->reference_price);

      // get the net change
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%f", &result->net_change);

      // get the percent change
      getline(&buffer, &n, f);
      getline(&buffer, &n, f);
      remove_tags(&buffer);
      remove_commas(&buffer);
      sscanf(buffer, "%f", &result->percent_change);

      endloop = 1;
    }
  }

  fclose(f);
  free(buffer);

  return result;
}