C: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 23: Zeile 23:
#include "time.h"
#include "time.h"
typedef unsigned long seed_t;
typedef unsigned long seed_t;
typedef struct {
#define DEFINE_VARS(seed, x, y, z, c) seed_t seed = 0x20111958UL; \
seed_t m_seed;
seed_t x = 0x33221122UL; \
seed_t m_factor;
seed_t y = 0x73829382UL; \
seed_t m_offset;
seed_t z = 0xabcdef07UL; \
seed_t m_x;
seed_t c = 0
seed_t m_y;
#define next(seed, x, y, z, c) \
seed_t m_z;
do { \
seed_t m_c;
x = x * 0x20111957 + 0x11121989; \
} Random;
y ^= y << 13; \
 
y ^= y >> 17; \
void init(Random* rand){
y ^= y << 5; \
rand->m_seed = 0x20111958UL;
seed_t t = 698769069UL * z + c; \
rand->m_factor = 0x323abceUL;
c = t << 32; \
rand->m_offset = 0x1234321UL;
z = t; \
rand->m_x = 0x33221122UL;
seed = x + y + z; \
rand->m_y = 0x73829382UL;
} while (0)
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){
seed_t calcRandom(int millions){
Random rand;
DEFINE_VARS(seed, x, y, z, c);
seed_t rc = 0;
seed_t rc = 0;
int ix;
init(&rand);
while (millions-- > 0){
while (millions-- > 0){
int ix2 = 1000000;
int ix2 = 1000000;
while(ix2-- > 0)
while(ix2-- > 0){
rc += next(&rand);
next(seed, x, y, z, c);
rc += seed;
}
}
}
return rc;
return rc;

Version vom 13. Mai 2016, 22:17 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;
#define DEFINE_VARS(seed, x, y, z, c) seed_t 	seed = 0x20111958UL; \
	seed_t x = 0x33221122UL; \
	seed_t y = 0x73829382UL; \
	seed_t z = 0xabcdef07UL; \
	seed_t c = 0
#define next(seed, x, y, z, c) \
	do { \
		x = x * 0x20111957 + 0x11121989; \
		y ^= y << 13; \
		y ^= y >> 17; \
		y ^= y << 5; \
		seed_t t = 698769069UL * z + c; \
		c = t << 32; \
		z = t; \
		seed = x + y + z; \
	} while (0)
seed_t calcRandom(int millions){
	DEFINE_VARS(seed, x, y, z, c);
	seed_t rc = 0;
	while (millions-- > 0){
		int ix2 = 1000000;
		while(ix2-- > 0){
			next(seed, x, y, z, c);
			rc += seed;
		}
	}
	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);
}