Exemplo n.º 1
0
void IvtSearch(void){
	char keyword[50];
	strcpy(keyword,"");
	FILE *rFile = fopen("Ivt_Idx","r");
	if ( NULL == rFile ){
        printf( "IvT_Idx Table is not exist." );
        printf( "Please Using CreatIvtTable(void) to build it." );
        return;
    }

	printf("Please Input the word that you want to search: ");
	scanf("%s",keyword);
	printf("===============================================================\n");

	struct timespec start, end;
	clock_gettime(CLOCK_REALTIME, &start);
	char buffer[1024];
	strcpy(buffer,"");
	while (!feof(rFile)){
		fgets(buffer,1024,rFile);
		char word[50];
		int i = 0;
		for(i=0;buffer[i] != '\n' && buffer[i] != '\0'; i++)
			word[i] = buffer[i];

		word[i] = '\0';

		if(strcasecmp(keyword, word) == 0){
			fgets(buffer,1024,rFile);
			printf("fequence : %s", buffer);
			printf("Article No : \n");
			while(1){
				char temp;
				fscanf(rFile,"%c",&temp);
				if(temp == ' ')
					printf("\n");
				else if(temp == '\n'){
					clock_gettime(CLOCK_REALTIME, &end);
					printf("Search time : %lf\n",diff_in_second(start, end));
					return;
				}
				else
					printf("%c",temp);
			}
		}
	}

	printf("Can't find %s in any article.\n",keyword);
	clock_gettime(CLOCK_REALTIME, &end);
	printf("Search time : %lf\n",diff_in_second(start, end) );

}
Exemplo n.º 2
0
int main()
{
    FILE *fp;
    struct timespec start, end;
    char buffer[1024];
    int i = 0,c=0;
    double cpu_time1,cpu_time2;
    fp = fopen("str_array.txt","r");
    if( fp == NULL)
        printf("Open failure\n");
    else {
        for(c = fgetc(fp); c != EOF ; i++) {
            buffer[i] = c;
            c = fgetc(fp);
        }
    }
    fclose(fp);
    char str[i];
    strncpy(str,buffer,i);
    int k;
    char x;
    FILE *fw = fopen("orig_time_recursive.txt", "w");
    FILE *fw2 = fopen("orig_time_iterative.txt", "w");
    for(k=97; k<=122; k++) {

        int j;
        clock_gettime(CLOCK_REALTIME, &start);
        for( j=0; j<1000; j++) {
            smallest_character_recursive(str,k,i);
        }
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time1 = diff_in_second(start, end);
        clock_gettime(CLOCK_REALTIME, &start);
        for( j=0; j<1000; j++) {
            x=smallest_character_iterative(str,k);
        }
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time2 = diff_in_second(start, end);
        printf("execution time of recursive() : %lf sec   answer=%c \n", cpu_time1,x);
        printf("execution time of iterative() : %lf sec\n", cpu_time2);
        fprintf(fw, "%d %lf\n",k-96, cpu_time1);
        fprintf(fw2, "%d %lf\n",k-96, cpu_time2);

    }
    fclose(fw);
    fclose(fw2);
    return 0;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    int i, j;
    char c, result;
    struct timespec start, end;
    double cpu_time;
    FILE* fp;

    /* check file opening */
    char a[10000000+10];
    fp = fopen("pattern.txt", "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }
    fscanf(fp,"%s\n",a);

    /* Find smallest character */
    c = 'z'+1;
    clock_gettime(CLOCK_REALTIME, &start);
    result = q2_smallest_character_nr(a, c);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    printf("size=%8d (%c,%c) execution time: %10lf\n",strlen(a), c, result, cpu_time);

    return 0;
}
Exemplo n.º 4
0
int main()
{
    char str[] = {'c', 'f', 'j', 'p', 'v'};
    char c[3] = {'a', 'c', 'Z'};
    char result[3];
    struct timespec start, end;

    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);

    int i;
    for (i = 0; i < 10000; ++i) {
        result[0] = smallest_character(str, c[0]);
        result[1] = smallest_character(str, c[1]);
        result[2] = smallest_character(str, c[2]);
    }

    clock_gettime(CLOCK_REALTIME, &end);
    double cpu_time = diff_in_second(start, end);

    printf("result[0]: %c\n", result[0]);
    printf("result[1]: %c\n", result[1]);
    printf("result[2]: %c\n", result[2]);
    printf("execution time: %lf sec\n", cpu_time);

    return 0;
}
Exemplo n.º 5
0
Arquivo: main.c Projeto: j0486280/quiz
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0,out;
    char line[MAX_INPUT_SIZE];
    struct timespec start, end;
    double cpu_time;
    int argu [LISTNODE_MAX_LENGTH];
    int inputarray_len;

    fp = fopen(DATA_PATH, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        inputarray_len = getIntArray(line,argu);
        assert( inputarray_len < LISTNODE_MAX_LENGTH && "Array length overflow !!!");
        clock_gettime(CLOCK_REALTIME, &start);
        out = maxSubArray(argu,inputarray_len);
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time += diff_in_second(start, end);
        printf("answer : %d\n",out);
    }

    printf("execution time : %lf sec\n", cpu_time);

    return 0;
}
Exemplo n.º 6
0
int main()
{
    FILE *file = fopen("q2_iterative.txt", "a");
    double cpu_time1;
    struct timespec start, end;
    srand(time(NULL));
    int random_len = (rand() % 10) + 1;
    char i;
    static const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
    char str[random_len];
    struct sched_param param;
    int maxpri;
    maxpri = sched_get_priority_max(SCHED_FIFO);//max=99, min=1
    if(maxpri == -1) {
        perror("sched_get_priority_max() failed");
        exit(1);
    }
    param.sched_priority = maxpri;
    if (sched_setscheduler(getpid(), SCHED_FIFO, &param) == -1) {
        perror("sched_setscheduler() failed");
        exit(1);
    }
    memset(str, 0, sizeof(str));
    for(i=0; i<random_len; i++) str[i] = alpha[rand() % (strlen(alpha) - 1)];
    for(i=97; i<123; i++) { //test from a to z
        clock_gettime(CLOCK_REALTIME, &start);
        assert(smallest_character(str,i));
        clock_gettime(CLOCK_REALTIME, &end);
        printf("Output[%c]: %c\n",i, smallest_character(str,i));
        cpu_time1 = diff_in_second(start, end);
        fprintf(file, "%f\n", cpu_time1);
    }
    return 0;
}
Exemplo n.º 7
0
int main()
{
    FILE *file = fopen("q4_recursive.txt", "a");
    double cpu_time1;
    struct timespec start, end;

    FILE *fp = fopen("test.txt","r");
    char txtEachLine[700];
    char txtAns[100];
    while(fgets(txtEachLine, sizeof(txtEachLine), fp)) {
        txtEachLine[strlen(txtEachLine)-1] = '\0';
        int arr[200];
        int index = 0;
        char *pch = strtok(txtEachLine,",");
        while(pch) {
            arr[index++] = atoi(pch);
            pch = strtok(NULL, ",");
        }
        int ans = 0;
        fgets(txtAns, sizeof(txtAns), fp);
        txtAns[strlen(txtAns)-1] = '\0';
        ans = atoi(txtAns);
        clock_gettime(CLOCK_REALTIME, &start);
        assert(maxSubArray(arr,index) == ans);
        clock_gettime(CLOCK_REALTIME, &end);

        cpu_time1 = diff_in_second(start, end);
        printf("execution time: %lf sec\n", cpu_time1);
        fprintf(file, "%f\n", cpu_time1);
    }
    return 0;
}
Exemplo n.º 8
0
int main()
{
    struct timespec start, end;
    double cpu_time;
    LinkNode *head = CreateNode();
    assert((head->next != NULL && head->next->next !=NULL) && "no cycle");
    clock_gettime(CLOCK_REALTIME, &start);
    LinkNode *res = detectCycle(head);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    //use print to check the list 
    /*printf("%d->%d->%d->%d->%d->%d->%d\n",head->data,
				    head->next->data,
				    head->next->next->data,
				    head->next->next->next->data,
				    head->next->next->next->next->data,
				    head->next->next->next->next->next->data,
				    head->next->next->next->next->next->next->data);*/
    if (res != NULL)
	//printf("result: n%d\n",res->data);
	printf("execution time: %.10lf sec\n", cpu_time);
    else
	printf("NULL\n");
    return 0;
}
Exemplo n.º 9
0
int main()
{
    struct timespec start, end;

	TreeNode *root = NULL;
	root = creatTree(root);

	/* assert - bounds checking */
	assert(root != NULL &&	"Root is null!");

    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);

    int i;
    for (i = 0; i < 10000; ++i) {
		flatten(root);
	}

    clock_gettime(CLOCK_REALTIME, &end);
    double cpu_time = diff_in_second(start, end);

	printTree(root);
    printf("execution time: %lf sec\n", cpu_time);

    return 0;
}
Exemplo n.º 10
0
int main()
{
	struct timespec start, end;
    double cpu_time;
    clock_gettime(CLOCK_REALTIME, &start);
    for (int j = 0; j < LOOP; j++) {

	int num[]= {1,2,5,3,4,0,6};
	NODE *root=NULL;
	root = node_setting(root,num,0);
	printf("before\n");
	print_tree(root);
    
    flatten(root);
	printf("affter\n");
	print_tree(root);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = (double) diff_in_second(start, end) / LOOP;

    FILE *fp;
    fp = fopen("time.dat", "a");
    assert(fp);

    fprintf(fp, "method\ttime\n");
    fprintf(fp, "iterative\t%.9lf\n", cpu_time);

    fclose(fp);

    printf("execution time : %lf sec\n", cpu_time);

    return 0;
}
Exemplo n.º 11
0
int main()
{
	double endtime = 0;
    double total_time = 0;
    struct timespec start, end;
    int arr[]= {-2,1,-3,4,-1,2,1,-5,4};
    int len = sizeof(arr) / sizeof(arr[0]);

    for(int i = 0; i < LOOP_TIME; i++){
    	clock_gettime(CLOCK_REALTIME, &start);
        maxSubArray(arr, len);
        clock_gettime(CLOCK_REALTIME, &end);
        endtime =  diff_in_second(start, end);
        total_time += endtime;
    }

    FILE *fp;
    fp = fopen("time.dat","a");
    fprintf(fp, "Method\tTime\n");
#if defined(ITER)
    fprintf(fp, "iterative\t%.9lf\n",total_time);
#endif

#if defined(REC)
    fprintf(fp, "recursive\t%.9lf\n",total_time);
#endif    
	fclose(fp);
	printf("Total execution time is %.9lf\n", total_time);
    return 0;
}
Exemplo n.º 12
0
void main()
{
        int i;
        int findCnt;
        list_t*  listShow= NULL;
        
        char *find[]={"hello","home"};
        
        int wordsToHash = 20;//Remember to define your size!!!(how many phrase  is s[] )!

        static char *s[]={"steve","bOB","apple","ban","Johnson", 
        "banana","ice","happy","home","hello","love","wen","danny"
        ,"dog","hot" ,"cold","fato","fatrabbit","jerry","tux"};
        
        //timer
        hash_table_t *my_hash_table;
        clock_t         start, stop;
        struct timespec go, end;
        double cpu_time1;


        int size_of_table = 12;
        clock_gettime(CLOCK_REALTIME, &go);
        start = clock();
        my_hash_table = create_hash_table(size_of_table);
        
        
       
        //hashing   Remember
        for( i=0; i<wordsToHash ;i++){
        add_string(my_hash_table,s[i]);
        }

        listShow = lookup_string(my_hash_table,find[1]);


        stop = clock();
        clock_gettime(CLOCK_REALTIME, &end);
        cpu_time1 = diff_in_second(go, end);
        float   elapsedTime = (float)(stop - start) /(float)CLOCKS_PER_SEC * 1000.0f;
        printf( "Time to hash:  %3.1f ms\n", elapsedTime );
        printf("execution time of cpu : %lf sec\n", cpu_time1);
        printf("Found the word list \n");
        printList(listShow);
        printf("Found the word is %s \n",listShow->str);
        printf("-------------------------- \n");  



        printf("Print hash table content \n");
        for(i=0; i<size_of_table ;i++)
        {
         printList(my_hash_table->table[i]);
        printf("\n");
        }

        free_table(my_hash_table);
}
Exemplo n.º 13
0
void *reader(void *ptr)
{
        int spaceloop, countloop;
        //loop = *(int *)ptr;
        while(1)
        {
            dequeue();
            sem_getvalue(&spacesem,&spaceloop);
            sem_getvalue(&countsem,&countloop);
            if (spaceloop == NUM_OF_CELL && countloop == 0)
                break;
            //loop--;
        }
        fflush(log_file);
        fclose(log_file);
        clock_get_monotonic_time(&end_reader);
        printf("Writer elapsed time: %fs\n", diff_in_second(start, end_writer));
        printf("Reader elapsed time: %fs\n", diff_in_second(start, end_reader));
        return NULL;
}
Exemplo n.º 14
0
int main()
{
    struct timespec start, end;
    double cpu_time;

    clock_gettime(CLOCK_REALTIME, &start);

    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);

    printf("execution time of iterative : %lf sec\n", cpu_time);
    return 0;
}
Exemplo n.º 15
0
Arquivo: main.c Projeto: r930709/quiz
int main()
{
    char line[64],*str;
    int A[]= {-2,1,-3,4,-1,2,1,-5,4};
    int sum;
    struct timespec start,end;
    double cpu_time1;
    //FILE *fp;
    int i=0;
    for(i=0; i<sizeof(A)/sizeof(A[0]); i++) {
        printf("%t\n",A[i]);
    }
#if defined(ITER)
    clock_gettime(CLOCK_REALTIME, &start);
    sum = MaxSubArray_Iterative(A,(sizeof(A)/sizeof(A[0])));
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start,end);
#endif
#if defined(RECUR)
    clock_gettime(CLOCK_REALTIME, &start);
    sum = MaxSubArray_Recursive(A,(sizeof(A)/sizeof(A[0])));
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start,end);
#endif

#if defined(ITER)
    printf("sum = [ %d ]\n",sum);
    printf("execution time of iterative() %.10lf sec\n",cpu_time1);
#endif
#if defined(RECUR)
    printf("sum = [ %d ]\n",sum);
    printf("execution time of recursive() %.10lf sec\n",cpu_time1);
#endif


    return(0);
}
Exemplo n.º 16
0
int main()
{

    ListNode* head;
    ListNode* cursor;
    ListNode* begin;

    head = malloc(sizeof(ListNode));
    head->key = 1;
    head->next= malloc(sizeof(ListNode));

    cursor = head->next;
    cursor->key = 2;
    cursor->next = malloc(sizeof(ListNode));
    begin = head;

    cursor = cursor->next;
    cursor->key = 3;
    cursor->next = malloc(sizeof(ListNode));

    cursor = cursor->next;
    cursor->key = 4;
    cursor->next = malloc(sizeof(ListNode));

    cursor = cursor->next;
    cursor->key = 5;
    cursor->next = malloc(sizeof(ListNode));

    cursor = cursor->next;
    cursor->key = 6;
    cursor->next = begin;

    struct timespec start, end;
    double cpu_time = 0;
    clock_gettime(CLOCK_REALTIME, &start);
    cursor = q5_detectCycle_r(head,head,0);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);

    if(!cursor)
        printf("%s", "No cycle");
    else
        printf("Cycle begin at %d",cursor->key);
    printf(" execution time: %8lf\n", cpu_time);

    return 0;
}
Exemplo n.º 17
0
int main()
{
    struct timespec start, end;
    double cpu_time;

    int array[9] = { -2,1,-3,4,-1,2,1,-5,4 };
    int max;

    clock_gettime(CLOCK_REALTIME, &start);
    max = maxSubArray(array,9);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);

    printf("ans = %d execution time = %lf nsec\n", max, cpu_time);

    return 0;
}
Exemplo n.º 18
0
int main(int argc, char *argv[])
{
    struct timespec start, end;
    double cpu_time;
    
    double pi;
	
    assert(compute_pi(128*1024*1024) &&"Did you implement compute_pi()");
    
    clock_gettime(CLOCK_REALTIME, &start);
    pi=compute_pi((size_t)128*1024*1024);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time = diff_in_second(start, end);
    printf("pi: %lf\n",pi);
    printf("execution time of compute_pi() : %lf sec\n", cpu_time);

    return 0;
}
Exemplo n.º 19
0
int main()
{
   struct timespec start, end;
    double cpu_time;
    char str[][30]={"   ","!!#$)+/","023589",":<=@","CDEFJMNPSV","[]^_","cdefjmnpsv","  !!235===DDDEcccggssv"};
    char c;
    for(int j=0;j<7;++j){
      printf("The sorted character array is %s \n",str[j]);
      printf("Enter the search character : ");
      scanf("%c",&c);
      //c = getche();
      clock_gettime(CLOCK_REALTIME, &start);
      assert(printf("%c\n",smallest_character(str[j], c)));
      clock_gettime(CLOCK_REALTIME, &end);
      cpu_time = diff_in_second(start, end);
      printf("execution time of iterative : %lf sec\n\n", cpu_time);
    }
}
Exemplo n.º 20
0
int main()
{
    char str[10];
    char c;
    struct timespec start, end;
    double cpu_time1;

    printf("Enter the SORTED string : ");
    gets(str);
    printf("Enter the search character : ");
    scanf("%c",&c);
    clock_gettime(CLOCK_REALTIME, &start);
    fflush(stdin);
    printf("%c\n",smallest_character(str, c));
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);
    printf("execution time of recursive : %lf sec\n", cpu_time1);
    return 0;
}
Exemplo n.º 21
0
Arquivo: main.c Projeto: LanKuDot/quiz
int main( int argc, char *argv[] )
{
	/* There must be 3 arguments. */
	assert( argc == 3 && "Input format: <program> <sorted_str> <search character>" );
	
#ifdef MEASURE_TIME
	struct timespec start, end;

	clock_gettime( CLOCK_REALTIME, &start );
	smallest_character( argv[1], argv[2][0] );
	clock_gettime( CLOCK_REALTIME, &end );
#endif

	printf( "%c\n", smallest_character( argv[1], argv[2][0] ) );
#ifdef MEASURE_TIME
	printf( "Time for smallest_character(): %.9lf sec\n", diff_in_second( start, end ) );
#endif

	return 0;
}
Exemplo n.º 22
0
int main()
{
    FILE *file = fopen("q3_recursive.txt", "a");
    double cpu_time1;
    struct timespec start, end;
    struct TreeNode *a;
    a = malloc(sizeof(struct TreeNode));
    a->right=NULL;
    a->left=NULL;
    CreateNode(a,"",0);
    int nodeNum = 10;
    BuildRandomTree(a,nodeNum);
    clock_gettime(CLOCK_REALTIME, &start);
    flatten(a);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);
    freeNode(a);
    printf("execution time: %lf sec\n", cpu_time1);
    fprintf(file, "%f\n", cpu_time1);
    return 0;
}
Exemplo n.º 23
0
int main()
{
    struct timespec start, end;
    double cpu_time1;
    clock_gettime(CLOCK_REALTIME, &start);
    struct TreeNode *root  = newNode(1);
    root->left             = newNode(2);
    root->right           = newNode(5);
    root->left->left     = newNode(3);
    root->left->right   = newNode(4);
    root->right->right  = newNode(6);

    printf("\nflattened binary tree is \n");
    flatten(root);
    printTree(root);
    printf("\n");
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);
    printf("execution time of iterative : %lf sec\n", cpu_time1);

    return 0;
}
Exemplo n.º 24
0
int main()
{
    printf("pid: %d\n", getpid());
    uint8_t *pixels;
    light_node lights = NULL;
    rectangular_node rectangulars = NULL;
    sphere_node spheres = NULL;
    color background = { 0.0, 0.1, 0.1 };
    struct timespec start, end;

#include "use-models.h"

    /* allocate by the given resolution */
    pixels = malloc(sizeof(unsigned char) * ROWS * COLS * 3);
    if (!pixels) exit(-1);

    printf("# Rendering scene\n");
    /* do the ray tracing with the given geometry */
    clock_gettime(CLOCK_REALTIME, &start);
    raytracing(pixels, background,
               rectangulars, spheres, lights, &view, ROWS, COLS);
    clock_gettime(CLOCK_REALTIME, &end);
    {
        FILE *outfile = fopen(OUT_FILENAME, "wb");
        write_to_ppm(outfile, pixels, ROWS, COLS);
        fclose(outfile);
    }

    delete_rectangular_list(&rectangulars);
    delete_sphere_list(&spheres);
    delete_light_list(&lights);
    free(pixels);
    printf("Done!\n");
    printf("Execution time of raytracing() : %lf sec\n", diff_in_second(start, end));
    return 0;
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    PHONE_BOOK.initialize();
    printf("size of entry : %u bytes\n", sizeof(entry));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif

    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        PHONE_BOOK.new_node(line, e);
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    PHONE_BOOK.move_node();

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";


    //assert(PHONE_BOOK.findName(input, e) &&
    //       "Did you implement findName() in " IMPL "?");
    //assert(0 == strcmp(PHONE_BOOK.findName(input, e) -> lastName, "zyxel"));

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    PHONE_BOOK.findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    /* FIXME: release all allocated entries */
    PHONE_BOOK.release();
    return 0;
}
Exemplo n.º 26
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }

    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;

    entry *hash_table[1000] = {NULL},*hash_head[1000]= {NULL};


#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    clock_gettime(CLOCK_REALTIME, &start);
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
#if defined(OPT)
        unsigned int key;
        key = hash(line);
        key = key % 1000;

        if(hash_head[key] == NULL) {
            hash_head[key] = (entry*)malloc(sizeof(entry));

            hash_table[key] = hash_head[key];


        }
        hash_table[key] = append(line,hash_table[key]);

#else

        e = append(line, e);
#endif
    }
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    /* close file as soon as possible */
    fclose(fp);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";

#if defined(OPT)

    unsigned int k = hash(input) % 1000;
    e = hash_head[k];




#else
    e = pHead;
#endif
    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));


#if defined(OPT)

    __builtin___clear_cache((char *) hash_head, (char *) hash_head + 1000 * sizeof(entry));
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);

    FILE *output;
#if defined(OPT)
    output = fopen("opt.txt", "a");
#else
    output = fopen("orig.txt", "a");
#endif
    fprintf(output, "append() findName() %lf %lf\n", cpu_time1, cpu_time2);
    fclose(output);

    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);

    if (pHead->pNext) free(pHead->pNext);
    free(pHead);

    return 0;
}
Exemplo n.º 27
0
int main(int argc, char *argv[])
{
    FILE *fp;
    int i = 0;
    char line[MAX_LAST_NAME_SIZE];
    struct timespec start, end;
    double cpu_time1, cpu_time2;

    /* check file opening */
    fp = fopen(DICT_FILE, "r");
    if (fp == NULL) {
        printf("cannot open the file\n");
        return -1;
    }
#ifndef PHONEBOOKOPT
    /* build the entry */
    entry *pHead, *e;
    pHead = (entry *) malloc(sizeof(entry));
    printf("size of entry : %lu bytes\n", sizeof(entry));
    e = pHead;
    e->pNext = NULL;
#else
    threadArg *threadData = malloc(sizeof(struct __THREAD_ARG));
    void *status;
    hash *e;
    hash *pHead = malloc(26 * sizeof(hash));
    for ( int i = 0 ; i < 26 ; i++ )
        pHead[i].pNamelist = NULL;
    printf("size of entry : %lu bytes\n", sizeof(pHead[0]));
    e = pHead;
    threadData ->fp = fp;
    threadData ->e = e;
#endif

#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif

    clock_gettime(CLOCK_REALTIME, &start);
#ifndef PHONEBOOKOPT
    while (fgets(line, sizeof(line), fp)) {
        while (line[i] != '\0')
            i++;
        line[i - 1] = '\0';
        i = 0;
        e = append(line, e);
    }

#else
    pthread_attr_t attr;
    pthread_mutex_init(&mutexsum, NULL);

    /* Create threads */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for (long t = 0 ; t < NUMTHREADS ; t++) {
        pthread_create(&callThd[t], &attr, import, (void *)threadData);
    }

    pthread_attr_destroy(&attr);

    for (long t = 0 ; t < NUMTHREADS ; t++) {
        pthread_join(callThd[t], &status);
    }

#endif
    /* close file as soon as possible */
    fclose(fp);

    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time1 = diff_in_second(start, end);

    e = pHead;

    /* the givn last name to find */
    char input[MAX_LAST_NAME_SIZE] = "zyxel";
    e = pHead;

    assert(findName(input, e) &&
           "Did you implement findName() in " IMPL "?");
    assert(0 == strcmp(findName(input, e)->lastName, "zyxel"));
#if defined(__GNUC__)
    __builtin___clear_cache((char *) pHead, (char *) pHead + sizeof(entry));
#endif
    /* compute the execution time */
    clock_gettime(CLOCK_REALTIME, &start);
    findName(input, e);
    clock_gettime(CLOCK_REALTIME, &end);
    cpu_time2 = diff_in_second(start, end);
    printf("execution time of append() : %lf sec\n", cpu_time1);
    printf("execution time of findName() : %lf sec\n", cpu_time2);
    /* FIXME: release all allocated entries */
    free(pHead);
    pthread_mutex_destroy(&mutexsum);
    pthread_exit(NULL);
    return 0;
}
Exemplo n.º 28
0
void SequSearch(void){
	char buffer[1024];
	FILE *rFile;
	rFile = fopen("en09062011.news","r");

	char keyword[50];
	strcpy(keyword,"");
	printf("Please Input the word that you want to search: ");
	scanf("%s",keyword);
	printf("===============================================================\n");

	struct timespec start, end;
	clock_gettime(CLOCK_REALTIME, &start);
	Article_ *arc = (Article_ *)malloc(sizeof(Article_));
	arc->No = -1;
	arc->pNext = NULL;
	int ArticleNum = 0;
	while (fgets(buffer, 1024, rFile) != NULL){
		int idx=0;
		char http[4];
		for(idx=0;idx<4;idx++)
			http[idx] = buffer[idx];

		http[idx] = '\0';


		if((strcasecmp(http, "http")) == 0){
			int i=0;
			for (int i=0; i<5; i++ ){
				fgets(buffer, 1024, rFile);
			}
			ArticleNum++;
		}
		else{
			char word[25] = "";
			int word_idx = 0;
    		for (idx=0; buffer[idx] != '\0'; idx++){
    			if (buffer[idx] >= 97 && buffer[idx] <= 122){
    				word[word_idx++] = buffer[idx];
    			}
				else{
					word[word_idx] = '\0';
					if (strcmp(word, "") != 0){
						if (strcasecmp(keyword,word) == 0){
							Article_ *temp = arc;
							int IsNew = 1;
							while (temp != NULL){
								if(temp->No == ArticleNum)
									IsNew = 0;
								if(temp->pNext != NULL)	
									temp = temp->pNext;
								else
									break;
							}
							if(IsNew == 1){
								temp->pNext = (Article_ *)malloc(sizeof(Article_));
								temp = temp->pNext;
								temp->No = ArticleNum;
								temp->pNext = NULL;
							}
						}
					}

					word_idx = 0;
					strcpy(word, "");
       			}
    		}
    	}
	}
	printf("Article No :");
	Article_ *temp = arc;
	while (temp != NULL){
		printf("%d\n",temp->No);
		temp = temp->pNext;
	}
	clock_gettime(CLOCK_REALTIME, &end);
	printf("Search time : %lf\n",diff_in_second(start, end) );
}
Exemplo n.º 29
0
int main()
{
	int sockfd, numbytes;
	struct sockaddr_in my_addr;
	struct sockaddr_in their_addr;
	struct stat filestat;
	off_t success_bytes = 0, success_rate;

	double cpu_time;
	struct timespec start, end;

	uint32_t payload_size = 100;
	uint8_t* data_out = (uint8_t*) malloc(payload_size);

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )
		error("socket");

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(5134);
	my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1 )
		error("bind");

	printf("Server is ready to receive\n");

	FILE *fp;
	if ((fp = fopen("getfile", "w")) == NULL)
		error("fopen");

	/* read file size from client to know how much data remained */
	if (recv(sockfd, &filestat.st_size, sizeof(filestat.st_size), 0) < 0)
		error("Error:read file_size failed");

	printf("The file size is %lu bytes\n\n", filestat.st_size);
	clock_gettime(CLOCK_REALTIME, &start);

	while (1) {

		numbytes = recv(sockfd, data_out, payload_size, 0);
		printf("read %d bytes\n", numbytes);

		numbytes = fwrite(data_out, sizeof(char), numbytes, fp);
		printf("fwrite %d bytes\n", numbytes);

		success_bytes += numbytes;
		success_rate = success_bytes * 100 / filestat.st_size;
		printf("success_bytes = %lu \n", success_bytes);
		printf("success_rate = %lu% \n", success_rate);

		clock_gettime(CLOCK_REALTIME, &end);
		cpu_time = diff_in_second(start, end);
		printf("execution time of transmission : %lf sec\n\n", cpu_time);

		if (filestat.st_size == success_bytes)
			break;

	} /* end of while (1) */

	printf("server finished\n");

	fclose(fp);
	free(data_out);
	close(sockfd);
	return 0;
}
Exemplo n.º 30
0
int main(void)
{
	struct timespec start, end;
	FILE *read_fp, *write_fp;
	int array[ARRAY_SIZE];
	int i, j;
	double diff;

	read_fp = fopen(RANDOM_FILE, "r");

	/* insertion sort */
	printf("insertion sort\n");
	write_fp = fopen("./insertion_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		insertion_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* selection sort */
	printf("selection sort\n");
	write_fp = fopen("./selection_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		selection_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* bubble sort */
	printf("bubble sort\n");
	write_fp = fopen("./bubble_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		bubble_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* heap sort */
	printf("heap sort\n");
	write_fp = fopen("./heap_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		heap_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* merge sort */
	printf("merge sort\n");
	write_fp = fopen("./merge_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		merge_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* quick sort */
	printf("quick sort\n");
	write_fp = fopen("./quick_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		quick_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	/* radix sort */
	printf("radix sort\n");
	write_fp = fopen("./radix_sort.txt", "w");
	for (i = 0; i <= ARRAY_SIZE; i += 250) {
		for (j = 0; j < i; j++)
			fscanf(read_fp, "%d", &array[j]);
		clock_gettime(CLOCK_REALTIME, &start);
		quick_sort(array, i);
		clock_gettime(CLOCK_REALTIME, &end);
		diff = diff_in_second(start, end);
		fprintf(write_fp, "%5d data: %f seconds\n", i, diff);
		rewind(read_fp);
	}
	/* print_array(array, ARRAY_SIZE); */

	fclose(read_fp);

	return 0;
}