Skip to content

Instantly share code, notes, and snippets.

@mertcangokgoz
Created February 16, 2016 13:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mertcangokgoz/fefea5f73916aab2b8ea to your computer and use it in GitHub Desktop.
Geçerli Random TC
/*
**
** derleme:
** gcc -O3 -DMAX_THREAD=2 tcnouretici.c -lpthread
**
** i7-4770k 8 thread
** ./a.out > /dev/null 0.02s user 0.01s system 473% cpu 0.008 total
** ./a.out > /dev/null 0.03s user 0.00s system 382% cpu 0.008 total
** ./a.out > /dev/null 0.03s user 0.00s system 484% cpu 0.007 total
** ./a.out > /dev/null 0.03s user 0.00s system 377% cpu 0.008 total
** ./a.out > /dev/null 0.02s user 0.00s system 397% cpu 0.007 total
** ./a.out > /dev/null 0.03s user 0.00s system 454% cpu 0.007 total
** ./a.out > /dev/null 0.03s user 0.00s system 337% cpu 0.008 total
** ./a.out > /dev/null 0.03s user 0.00s system 420% cpu 0.008 total
**
** i7-4770k 1 thread
** ./a.out > /dev/null 0.02s user 0.00s system 79% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 98% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 79% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 79% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 99% cpu 0.020 total
** ./a.out > /dev/null 0.01s user 0.01s system 80% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 80% cpu 0.020 total
** ./a.out > /dev/null 0.02s user 0.00s system 80% cpu 0.020 total
**
*/
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define MAX_TOTAL 100000000
#ifndef MAX_THREAD
#define MAX_THREAD 2
#endif
struct tcno_t {
unsigned char tcnos[MAX_TOTAL / MAX_THREAD][12];
};
unsigned long xorshf96(unsigned long *x,
unsigned long *y,
unsigned long *z) {
unsigned long t;
*x ^= *x << 16; *x ^= *x >> 5; *x ^= *x << 1;
t = *x; *x = *y; *y = *z;
return *z = t ^ *x ^ *y;
}
void *tcnouret(void *p) {
unsigned int i, j;
unsigned long t_toplam, c_toplam;
struct tcno_t *tcnos = (struct tcno_t *) p;
unsigned long x = rand(), y = rand(), z = rand();
for (i = 0; i < MAX_TOTAL / MAX_THREAD; ++i) {
t_toplam = c_toplam = 0;
for (j = 0; j < 4; ++j) {
t_toplam += (tcnos->tcnos[i][j*2] = (xorshf96(&x, &y, &z) % 9) + 1);
c_toplam += (tcnos->tcnos[i][j*2+1] = xorshf96(&x, &y, &z) % 10);
}
// bir tane daha tek gerekli (9. sayi)
t_toplam += (tcnos->tcnos[i][8] = xorshf96(&x, &y, &z) % 10);
tcnos->tcnos[i][9] = (t_toplam * 7 - c_toplam) % 10;
tcnos->tcnos[i][10] = (t_toplam + c_toplam + tcnos->tcnos[i][9]) % 10;
for (j = 0; j < 11; ++j) {
tcnos->tcnos[i][j] += 48;
}
tcnos->tcnos[i][11] = '\n';
}
return p;
}
int main (int argc, char *argv[])
{
unsigned int i;
pthread_t threads[MAX_THREAD];
struct tcno_t *tcnos = (struct tcno_t *)
malloc(sizeof(struct tcno_t) * MAX_THREAD);
srand(time(NULL));
// threadlari olustur
for (i = 0; i < MAX_THREAD; ++i) {
pthread_create(&threads[i], NULL, tcnouret, &tcnos[i]);
}
// threadlari main threadle birlestir
for (i = 0; i < MAX_THREAD; ++i) {
pthread_join(threads[i], NULL);
write(1, &tcnos[i], sizeof(struct tcno_t));
}
free(tcnos);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment