upload fibonacci2 and update big_fibonacci and fibonacci

main
xiaosuGW 3 years ago
parent a23352cf5d
commit a838bd6620

@ -1,24 +1,177 @@
#include <stdio.h>
#include <stdlib.h>
// #include "get_time.h"
volatile unsigned long g_v = 0;
unsigned long int
fib(unsigned long int n)
/*fib(unsigned long int n)
{
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
return fib(n + g_v - 1) + fib(n + g_v - 2);
}*/
fib(unsigned long int n)
{
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int
main(int argc, char **argv)
{
unsigned long n = 0, r,r2,r3;
char * array = NULL;
unsigned long n = 0, r,r2,r3,r4,r5,r6,r7,r8,r9,r10;
scanf("%lu", &n);
// unsigned long long st = get_time(), en;
r = fib(29);
r2 = fib(29);
r3 = fib(29);
r = fib(n);
r2 = fib(n+1);
r3 = fib(n+2);
r4 = fib(n+3);
r5 = fib(n+4);
r6 = fib(n+5);
r7 = fib(n+6);
r8 = fib(n+7);
r9 = fib(n+8);
r10 = fib(n+9);
/*switch(n) {
case 0: {
array = malloc(4 * 1024);
memset(array, 'a', 4 * 1024 - 1);
array[4 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 1: {
array = malloc(100 * 1024);
memset(array, 'b', 100 * 1024 - 1);
array[100 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 2: {
array = malloc(200 * 1024);
memset(array, 'g', 200 * 1024 - 1);
array[200 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 4: {
array = malloc(400 * 1024);
memset(array, 'c', 400 * 1024 - 1);
array[400 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 6: {
array = malloc(600 * 1024);
memset(array, 'd', 600 * 1024 - 1);
array[600 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 8: {
array = malloc(800 * 1024);
memset(array, 'e', 800 * 1024 - 1);
array[800 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 10:{
array = malloc(1000 * 1024);
memset(array, 'f', 1000 * 1024 - 1);
array[1000 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
default: printf("error input of n\n");
}*/
// en = get_time();
printf("%lu\n", r3);
printf("%lu \n", n+10);
printf("%lu \n", r10);
printf("%lu \n", r9);
printf("%lu \n", r8);
printf("%lu \n", r7);
printf("%lu \n", r6);
printf("%lu \n", r5);
printf("%lu \n", r4);
printf("%lu \n", r3);
printf("%lu \n", r2);
printf("%lu \n", r);
// print_time(st, en);
return 0;
}
/*
int
main(int argc, char **argv)
{
unsigned long n = 0, r = 0;
scanf("%lu", &n);
switch(n) {
case 0: {
char array[4 * 1024] = {0};
memset(array, 'a', 4 * 1024);
array[4 * 1024 - 1] = 0;
break;
}
case 1: {
char array[100 * 1024] = {'b'};
memset(array, 'b', 100 * 1024);
array[100 * 1024 - 1] = 0;
break;
}
case 2: {
char array[200 * 1024] = {'c'};
memset(array, 'c', 200 * 1024);
array[200 * 1024 - 1] = 0;
break;
}
case 4: {
char array[400 * 1024] = {'d'};
memset(array, 'd', 400 * 1024);
array[400 * 1024 - 1] = 0;
break;
}
case 6: {
char array[600 * 1024] = {'e'};
memset(array, 'e', 600 * 1024);
array[600 * 1024 - 1] = 0;
break;
}
case 8: {
char array[800 * 1024] = {'f'};
memset(array, 'f', 800 * 1024);
array[800 * 1024 - 1] = 0;
break;
}
case 10:{
char array[1000 * 1024] = {'g'};
memset(array, 'g', 1000 * 1024);
array[1000 * 1024 - 1] = 0;
break;
}
default: printf("error input of n\n");
}
// unsigned long long st = get_time(), en;
printf("%lu \n", r);
// print_time(st, en);
return 0;
}
*/

@ -1,22 +1,178 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "get_time.h"
volatile unsigned long g_v = 0;
unsigned long int
/*fib(unsigned long int n)
{
if (n <= 1) return n;
return fib(n + g_v - 1) + fib(n + g_v - 2);
}*/
fib(unsigned long int n)
{
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int
//int
//main(int argc, char **argv)
//{
// char * array = NULL;
// unsigned long n = 0, r;
// scanf("%lu", &n);
// unsigned long long st = get_time(), en;
// r = fib(n);
// en = get_time();
/*switch(n) {
case 0: {
array = malloc(4 * 1024);
memset(array, 'a', 4 * 1024 - 1);
array[4 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 1: {
array = malloc(100 * 1024);
memset(array, 'b', 100 * 1024 - 1);
array[100 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 2: {
array = malloc(200 * 1024);
memset(array, 'g', 200 * 1024 - 1);
array[200 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 4: {
array = malloc(400 * 1024);
memset(array, 'c', 400 * 1024 - 1);
array[400 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 6: {
array = malloc(600 * 1024);
memset(array, 'd', 600 * 1024 - 1);
array[600 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 8: {
array = malloc(800 * 1024);
memset(array, 'e', 800 * 1024 - 1);
array[800 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
case 10:{
array = malloc(1000 * 1024);
memset(array, 'f', 1000 * 1024 - 1);
array[1000 * 1024 - 1] = 0;
printf("%s\n", array);
break;
}
default: printf("error input of n\n");
}
free(array);*/
//printf("%lu\n", n);
// printf("%lu\n", n+1);
// printf("%lu\n", r);
//return 0;
//}
main(int argc, char **argv)
{
unsigned long n = 0, r;
scanf("%lu", &n);
// unsigned long long st = get_time(), en;
r = fib(n);
// en = get_time();
printf("%lu\n", r);
scanf("%lu", &n);
FILE *f = stdout;
// unsigned long long st = get_time(), en;
//r = fib(29);
// en = get_time();
switch(n) {
case 0: {
char array[4 * 1024] = {0};
memset(array, 'a', 4 * 1024);
array[4 * 1024 - 1] = 0;
//printf("%s\n", array);
fwrite(array, 1, 4 * 1024 - 1, f);
break;
}
case 1: {
char array[100 * 1024] = {'b'};
memset(array, 'b', 100 * 1024);
array[100 * 1024 - 1] = 0;
//printf("%s\n", array);
fwrite(array, 1, 100 * 1024 - 1, f);
break;
}
case 2: {
char array[200 * 1024] = {'c'};
memset(array, 'c', 200 * 1024);
array[200 * 1024 - 1] = 0;
fwrite(array, 1, 200 * 1024 - 1, f);
//printf("%s\n", array);
break;
}
case 4: {
char array[400 * 1024] = {'d'};
memset(array, 'd', 400 * 1024);
array[400 * 1024 - 1] = 0;
fwrite(array, 1, 400 * 1024 - 1, f);
//printf("%s\n", array);
break;
}
case 6: {
char array[600 * 1024] = {'e'};
memset(array, 'e', 600 * 1024);
array[600 * 1024 - 1] = 0;
fwrite(array, 1, 600 * 1024 - 1, f);
//printf("%s\n", array);
break;
}
case 8: {
char array[800 * 1024] = {'f'};
memset(array, 'f', 800 * 1024);
array[800 * 1024 - 1] = 0;
fwrite(array, 1, 800 * 1024 - 1, f);
//printf("%s\n", array);
break;
}
case 10:{
char array[1000 * 1024] = {'g'};
memset(array, 'g', 1000 * 1024);
array[1000 * 1024 - 1] = 0;
fwrite(array, 1, 1000 * 1024 - 1, f);
//printf("%s\n", array);
break;
}
default: printf("error input of n\n");
}
fclose(f);
//printf("%lu\n", n);
//printf("%lu\n", r);
// print_time(st, en);
return 0;
}

@ -0,0 +1,44 @@
#ifndef GET_TIME_H
#define GET_TIME_H
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#ifndef WASM
#ifndef CPU_FREQ
#define CPU_FREQ 1000
#endif
#endif
static unsigned long long
get_time()
{
#if 0
unsigned long long int ret = 0;
unsigned int cycles_lo;
unsigned int cycles_hi;
__asm__ volatile ("rdtsc" : "=a" (cycles_lo), "=d" (cycles_hi));
ret = (unsigned long long int)cycles_hi << 32 | cycles_lo;
return ret;
#else
struct timeval Tp;
int stat;
stat = gettimeofday(&Tp, NULL);
if (stat != 0) printf("Error return from gettimeofday: %d", stat);
return (Tp.tv_sec * 1000000 + Tp.tv_usec);
#endif
}
static inline void
print_time(unsigned long long s, unsigned long long e)
{
#if 0
printf("%llu cycs, %llu us\n", e - s, (e - s) / CPU_FREQ);
#else
fprintf(stdout, "%llu us\n", e - s);
#endif
}
#endif /* GET_TIME_H */

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
// #include "get_time.h"
unsigned long int
fib(unsigned long int n)
{
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
/*
int
main(int argc, char **argv)
{
unsigned long r = 0;
//scanf("%s", recv_buf);
r = fib(30);
printf("%lu\n", r);
return 0;
}*/
int
main(int argc, char **argv)
{
//char * recv_buf = malloc(1024 * 1024);
char recv_buf[1024 * 1024] = {0};
//memset(recv_buf, 0, 1024 * 1024);
unsigned long r = 0;
//scanf("%s", recv_buf);
r = read(0, recv_buf, 1024 * 1024);
//size_t rd = read(0, recv_buf, 1000*1024);
//if (rd <= 0) return -1;
// unsigned long long st = get_time(), en;
//r = fib(30);
// en = get_time();
printf("%lu\n", r);
// print_time(st, en);
return 0;
}

@ -0,0 +1,32 @@
#!/bin/sh
ITERS=$3
# before running this benchmark,
# copy fibonacci to fibonacci_native.out
testeach() {
tmp_cnt=${ITERS}
exe_relpath=$1
echo "${exe_relpath} ($2) for ${tmp_cnt}"
while [ ${tmp_cnt} -gt 0 ]; do
bench=$(echo $2 | $exe_relpath 2> /dev/null)
tmp_cnt=$((tmp_cnt - 1))
echo "$bench"
done
echo "Done!"
}
MAXNUM=$2
tmp1_cnt=${MAXNUM}
while [ ${tmp1_cnt} -gt 28 ]; do
testeach ./fibonacci_$1.out ${tmp1_cnt}
tmp1_cnt=$((tmp1_cnt - 1))
done
echo "All done!"
Loading…
Cancel
Save