/*
 * keisoku4.c:	Check the performance of calculation.
 * (C) 1994 Okoma Lab. KEIO University.
 * Compile this file with: % gcc -o keisoku4 keisoku4.c
 * Do NOT optimize with -O!!
 */

#include <stdio.h>
#include <time.h>
#define MAXITER 100000000

/* These two variables should be in the same storage class */
static long onesec = 0, count;
time_t t1;

void justsecond()
{
	time_t t0;
	time(&t0); t1=t0;
	while (t1==t0)
	  time(&t1);
}

void time1sec()
{
	time_t t0;
	justsecond();
	time(&t0); t1=t0;
	while (t1==t0) {	/* while condition	*/
		onesec++;	/* increment long int	*/
		time(&t1);	/* call time		*/
	}
}

float withmilisec(end)
  long end;
{
	time_t T0, T1;
	count=0;
	time(&T0); T1=T0;
	while (T1==T0) {
		count++;
		time(&T1);
	}
	return (float)(onesec-count) / (float)onesec + (float)(end-t1);
}

int main()
{
	short	s1, s2, s3, s4, s5;
	long	l1, l2, l3, l4, l5;
	float	f1, f2, f3, f4, f5;
	double	d1, d2, d3, d4, d5;
	time_t	t, k, end;
	float	lap1, lap2, lap3, lap4, lap5;
	time(&t);
	s5 = 10+(s4 = 10+(s3 = 10+(s2 = 10+(s1 = t))));
	s1=t, s2=10+s1, s3=10+s2, s4=10+s3, s5=10+s4;
	l1=t, l2=10+l1, l3=10+l2, l4=10+l3, l5=10+l4;
	f1=(float)t, f2=10.+f1, f3=10.+f2, f4=10.+f3, f5=10.+f4;
	d1=(double)t, d2=10.+d1, d3=10.+d2, d4=10.+d3, d5=10.+d4;
	printf("\n        /**** Speed Test No.4 ****/\n\n");
	printf("   empty   short    long   float  double | total\n");

	time1sec();

	/* empty */
	for (k=0;k<MAXITER;k++)
	   ;
	time(&end);
	lap1 = withmilisec(end);
	printf("%8.2f", lap1); fflush(stdout);
	/* short */
	justsecond();
	for (k=0;k<MAXITER;k++)
	   s1=s1+s2-s3*s4/s5;
	time(&end);
	lap2 = withmilisec(end);
	printf("%8.2f", lap2); fflush(stdout);
	/* long */
	justsecond();
	for (k=0;k<MAXITER;k++)
	   l1=l1+l2-l3*l4/l5;
	time(&end);
	lap3 = withmilisec(end);
	printf("%8.2f", lap3); fflush(stdout);
	/* float */
	justsecond();
	for (k=0;k<MAXITER;k++)
	   f1=f1+f2-f3*f4/f5;
	time(&end);
	lap4 = withmilisec(end);
	printf("%8.2f", lap4); fflush(stdout);
	/* double */
	justsecond();
	for (k=0;k<MAXITER;k++)
	   d1=d1+d2-d3*d4/d5;
	time(&end);
	lap5 = withmilisec(end);
	printf("%8.2f", lap5); fflush(stdout);
	d1=s1+l1+f1+d1; /* -O しても null code を出さない */
	printf("%8.2f\n", lap1+lap2+lap3+lap4+lap5, d1);
	exit((int)d1%255);
}

