Skip to content

Commit

Permalink
Merge pull request #64 from yomaytk/add-examples
Browse files Browse the repository at this point in the history
Add prime calculation example.
  • Loading branch information
yomaytk authored Nov 11, 2024
2 parents c1a153f + 591c956 commit ab052d9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/benchmarks/eratosthenes_sieve/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CC=clang-18
GCC=gcc

eratosthenes_sieve: main.c
@ARCH=$$(uname -m); \
if [ "$$ARCH" = "x86_64" ]; then \
$(CC) -static --target=aarch64-linux-gnu --gcc-toolchain=/usr --sysroot=/usr/aarch64-linux-gnu main.c -fuse-ld=lld -pthread; \
elif [ "$$ARCH" = "aarch64" ]; then \
$(GCC) -O3 -static main.c -o a_o3.aarch64; \
else \
echo "Unknown architecture"; exit 1; \
fi

clean:
rm *.out
Binary file added examples/benchmarks/eratosthenes_sieve/a.aarch64
Binary file not shown.
Binary file not shown.
88 changes: 88 additions & 0 deletions examples/benchmarks/eratosthenes_sieve/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_NUM 10000000

int digit_num(int num) {
int res = 0;
for (;;) {
if (num > 0) {
num /= 10;
res++;
} else {
break;
}
}
return res;
}

int mhex(int num) {
int res = 1;
for (;;) {
if (num / res > 9) {
res *= 10;
} else {
break;
}
}
return res;
}

static double second(void)

{
return ((double) ((double) clock() / (double) CLOCKS_PER_SEC));
}

int main() {

double t1 = second();

printf("t1: %f\n", t1);

double tt1 = second();

printf("tt1: %f\n", tt1);

int *nums = (int *) malloc(MAX_NUM * sizeof(int));
char s[100];

nums[0] = nums[1] = 0;
for (int i = 2; i < MAX_NUM; i++) {
nums[i] = 1;
}

// cal
for (int base = 2; base < 1000; base++) {
if (!nums[base]) {
continue;
}
for (int target = base * 2; target < MAX_NUM; target += base) {
nums[target] = 0;
}
}
int seek;
for (int i = 0; i < MAX_NUM; i++) {
if (!nums[i]) {
continue;
}
int mm = mhex(i);
seek = 0;
for (int j = i; j > 0;) {
s[seek++] = '0' + (j / mm);
j -= mm * (j / mm);
mm /= 10;
}
}
s[seek] = '\n';

double t2 = second();
printf("t2: %f\n", t2);

// // stdout
printf("max prime: %s", s);
printf("time: %f\n", t2 - tt1);
}

0 comments on commit ab052d9

Please sign in to comment.