Skip to content

Commit f7cef19

Browse files
committed
Added some benchmarking (temporarily)
1 parent 85d2d71 commit f7cef19

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

demo/test.c

+52-10
Original file line numberDiff line numberDiff line change
@@ -1571,16 +1571,39 @@ static int test_mp_is_small_prime(void)
15711571
return EXIT_FAILURE;
15721572
}
15731573

1574+
#ifdef BENCHMARK_SIEVE
1575+
#include <time.h>
1576+
#include <sys/time.h>
1577+
#define LTM_BILLION 1000000000
1578+
static uint64_t gettime(void)
1579+
{
1580+
struct timespec ts;
1581+
clock_gettime(CLOCK_MONOTONIC, &ts);
1582+
return (((uint64_t)ts.tv_sec) * LTM_BILLION + (uint64_t)ts.tv_nsec);
1583+
}
1584+
static void print_timer(const char *s, const uint64_t start, const uint64_t stop)
1585+
{
1586+
printf("%s%"PRIu64" min %"PRIu64" sec %"PRIu64" ms %"PRIu64" usec %"PRIu64" nsec\n", s,
1587+
((stop - start)/LTM_BILLION) / 60,
1588+
((stop - start)/LTM_BILLION) % 60,
1589+
((stop - start)/(LTM_BILLION/1000))%1000,
1590+
((stop - start)/(LTM_BILLION/1000000))%1000,
1591+
(((stop - start)%LTM_BILLION)%1000)
1592+
);
1593+
}
1594+
#endif
1595+
15741596
static int test_mp_next_small_prime(void)
15751597
{
15761598
mp_sieve sieve;
15771599
mp_sieve_prime ret, p;
15781600
mp_int primesum, t;
15791601
int e;
15801602
int i, test_size;
1581-
1582-
clock_t start, stop;
1583-
1603+
#ifdef BENCHMARK_SIEVE
1604+
uint64_t startgt, stopgt;
1605+
#endif
1606+
/* Jumping wildly to and fro */
15841607
mp_sieve_prime to_test[] = {
15851608
52, 137, 153, 179, 6, 153, 53, 132, 150, 65,
15861609
27414, 36339, 36155, 11067, 52060, 5741,
@@ -1600,6 +1623,7 @@ static int test_mp_next_small_prime(void)
16001623
1019879761, 72282701, 2048787577, 2058368113
16011624
};
16021625

1626+
/* TODO: No, remove 64-bit version, way too big and a bit unwieldy, too! */
16031627
#if ( (defined MP_64BIT) && (defined MP_SIEVE_USE_LARGE_SIEVE) )
16041628
/* primesum up to 2^64
16051629
$ time /home/czurnieden/GITHUB/primesum/primesum 2^64
@@ -1613,8 +1637,12 @@ static int test_mp_next_small_prime(void)
16131637
/* const char *primesum_64 = "3879578600671960388666457126750869198"; */
16141638

16151639
const char *primesum_64 = "208139436659661";
1640+
#else
1641+
#ifdef BENCHMARK_SIEVE
1642+
const char *primesum_32 = "425649736193687430";
16161643
#else
16171644
const char *primesum_32 = "202259606268580";
1645+
#endif
16181646
#endif
16191647

16201648
mp_sieve_init(&sieve);
@@ -1623,6 +1651,9 @@ static int test_mp_next_small_prime(void)
16231651
test_size = (int)(sizeof(to_test)/sizeof(mp_sieve_prime));
16241652

16251653
for (i = 0; i < test_size; i++) {
1654+
#ifdef BENCHMARK_SIEVE
1655+
startgt = gettime();
1656+
#endif
16261657
if ((e = mp_next_small_prime(to_test[i], &ret, &sieve)) != MP_OKAY) {
16271658
fprintf(stderr,"mp_next_small_prime failed with \"%s\" at index %d\n",
16281659
mp_error_to_string(e), i);
@@ -1633,11 +1664,17 @@ static int test_mp_next_small_prime(void)
16331664
(unsigned long)to_test[i], (unsigned long)ret, (unsigned long)tested[i]);
16341665
goto LBL_ERR;
16351666
}
1667+
#ifdef BENCHMARK_SIEVE
1668+
stopgt = gettime();
1669+
printf("Single random access for prime: %*u: ", 10, ret);
1670+
print_timer("",startgt, stopgt);
1671+
#endif
16361672
}
16371673

16381674
DOR(mp_init_multi(&primesum, &t, NULL));
1639-
start = clock();
1640-
/* Define -DMP_SIEVE_PRIME_MAX_SQRT=0x1ffffffllu to make it feasable */
1675+
#ifdef BENCHMARK_SIEVE
1676+
startgt = gettime();
1677+
#endif
16411678
#if ( (defined MP_64BIT) && (defined MP_SIEVE_USE_LARGE_SIEVE) )
16421679
for (p = 4293918720lu; ret < (mp_sieve_prime)4294997297lu;) {
16431680
DO(mp_next_small_prime(p, &ret, &sieve));
@@ -1646,18 +1683,23 @@ static int test_mp_next_small_prime(void)
16461683
DO(mp_add(&primesum, &t, &primesum));
16471684
}
16481685
#else
1649-
/* We build the whole sieve instead of just the last segment. Too slow for valgrind */
1686+
#ifdef BENCHMARK_SIEVE
1687+
for (p = 0ul; ret < (mp_sieve_prime)MP_SIEVE_BIGGEST_PRIME;) {
1688+
#else
16501689
for (p = 4293918720lu; ret < (mp_sieve_prime)MP_SIEVE_BIGGEST_PRIME;) {
1690+
#endif
16511691
DO(mp_next_small_prime(p, &ret, &sieve));
16521692
p = ret + 1;
16531693
mp_set_u32(&t, ret);
16541694
DO(mp_add(&primesum, &t, &primesum));
16551695
}
16561696
#endif
1657-
stop = clock();
1658-
(void)mp_fwrite(&primesum, 10, stdout);puts("");
1659-
printf("Time for primesum was %.2f seconds\n",
1660-
(double)(stop-start)/(double)CLOCKS_PER_SEC);
1697+
#ifdef BENCHMARK_SIEVE
1698+
stopgt = gettime();
1699+
printf("Sum of all primes between 0 and %u = ", MP_SIEVE_BIGGEST_PRIME);
1700+
DO(mp_fwrite(&primesum, 10, stdout));
1701+
print_timer(", computed in ",startgt, stopgt);
1702+
#endif
16611703

16621704
#if ( (defined MP_64BIT) && (defined MP_SIEVE_USE_LARGE_SIEVE) )
16631705
DO(mp_read_radix(&t, primesum_64, 10));

mp_is_small_prime.c

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ static int s_init_single_segment_with_start(mp_sieve_prime a,
277277
* Worst case runtime is: building a base sieve and a segment and
278278
* search the segment
279279
*/
280+
/* TODO: add ability to keep segments? */
280281
mp_err mp_is_small_prime(mp_sieve_prime n, mp_sieve_prime *result, mp_sieve *sieve)
281282
{
282283
int e = MP_OKAY;

0 commit comments

Comments
 (0)