C: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 57: Zeile 57:
}
}


seed_t random(int millions){
seed_t calcRandom(int millions){
Random rand;
Random rand;
seed_t rc = 0;
seed_t rc = 0;
Zeile 76: Zeile 76:
if (argc > 1 && atol(argv[1]) != 0)
if (argc > 1 && atol(argv[1]) != 0)
millions = atol(argv[1]);
millions = atol(argv[1]);
value = random(millions);
value = calcRandom(millions);
duration = clock() - start;
duration = clock() - start;
printf ("%.3f sec %.3f HamaMips %d\n",  duration / (double) CLOCKS_PER_SEC,
printf ("%.3f HamaMips %.3f sec value: %d\n",   
millions * (double) CLOCKS_PER_SEC / duration, value);
millions * (double) CLOCKS_PER_SEC / duration,  
duration / (double) CLOCKS_PER_SEC,
value);
}
}
</pre>
</pre>

Version vom 13. Mai 2016, 21:54 Uhr


big.c (schnell Platte füllen)

#include <stdio.h>
#include <stdlib.h>

int main(int count, char** arg){
  FILE* file = fopen("big.data", "w");
  int size = 0x10000 * 128;
  void* data = calloc(size, 1);
  int mByte = 0;
  while (fwrite(data, size, 1, file) > 0){
    mByte += size / 1024 / 1024;
    printf("%d MByte\n", mByte);
  }
  return 0;
}

CPU-Benchmark hamamips.c

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
typedef unsigned long seed_t;
typedef struct {
	seed_t m_seed;
	seed_t m_factor;
	seed_t m_offset;
	seed_t m_x;
	seed_t m_y;
	seed_t m_z;
	seed_t m_c;
} Random;

void init(Random* rand){
	rand->m_seed = 0x20111958UL;
	rand->m_factor = 0x323abceUL;
	rand->m_offset = 0x1234321UL;
	rand->m_x = 0x33221122UL;
	rand->m_y = 0x73829382UL;
	rand->m_z = 0xabcdef07UL;
	rand->m_c = 0;
}
long next(Random* rand){
	// linear congruence generator:
	rand->m_x = rand->m_x * rand->m_factor + rand->m_offset;
	// XorShift:
	rand->m_y ^= rand->m_y << 13;
	rand->m_y ^= rand->m_y >> 17;
	rand->m_y ^= rand->m_y << 5;
	// Multiply with carry:
	seed_t t = 698769069UL * rand->m_z + rand->m_c;
	rand->m_c = t << 32;
	rand->m_z = t;
	rand->m_seed = rand->m_x + rand->m_y + rand->m_z;
	return rand->m_seed;
}

seed_t calcRandom(int millions){
	Random rand;
	seed_t rc = 0;
	int ix;
	init(&rand);
	while (millions-- > 0){
		int ix2 = 1000000;
		while(ix2-- > 0)
			rc += next(&rand);
	}
	return rc;
}
int main(int argc, char** argv){
	clock_t start = clock();
	seed_t value;
	clock_t duration;
	int millions = 100;
	if (argc > 1 && atol(argv[1]) != 0)
		millions = atol(argv[1]);
	value = calcRandom(millions);
	duration = clock() - start;
	printf ("%.3f HamaMips %.3f sec value: %d\n",  
		millions * (double) CLOCKS_PER_SEC / duration, 
		duration / (double) CLOCKS_PER_SEC,
		value);
}