AvaSmith-CodingSamples / C / OptimizationMatrixChallenge - C / search_benchmark.c
search_benchmark.c
Raw
// Complete this main to benchmark the search algorithms outlined in
// the project specification

#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <search.h>
#include "search.h"


int main(int argc, char *argv[]){
 /* if(argc < 5){
    
    exit(1);
  }
  */


  //Create lists and arrays and tree with max length
  // max length is 2^ maxpow
  // You will print and complete mega searchs maxpow-minpow times
  // Therefore we need a mega for loop running maxpow-minpow times
  // repeats: JUST the number of times you repeat a search of all numbers
  //creates table with all of the pointer structs in it
  func_table_t cfuncs[] = {                            // table of comparison functions
  {.name="linnear_array_search", .func=(void*)linear_array_search, .create=(void*)make_evens_array, .destroy=(void*)free}, // castes required to coerce
  {.name="linkedlist_search"   , .func=(void*)linkedlist_search, .create=(void*)make_evens_list,.destroy=(void*)list_free}, // different types of functions
  {.name="binary_tree_search"     , .func=(void*)binary_tree_search, .create=(void*)make_evens_tree,.destroy=(void*)bst_free}, // to unifying type of struct
  {.name="binary_array_search" , .func=(void*)binary_array_search,.create=(void*)make_evens_array,.destroy=(void*)free}, // field
  {.name=NULL}                                         // 'null' terminate the table
};
//A table that will hold the funciton pointers to the functions specified by user
  func_table_t realFuncs[] = {
    {.name = NULL},
    {.name = NULL},
    {.name = NULL},
    {.name = NULL},
    {.name = NULL}
  };
  

  //getting arguments from console
  int minpow = atoi(argv[1]);
  int maxpow = atoi(argv[2]);
  int repeats = atoi(argv[3]);

  //creating "booleans" for console input
  int bt = 0; //binary tree
  int ll = 0; //linked list
  int ba = 0; //binary array
  int la = 0; //linked array


  //Setting up variables for calculations later NO LONGER NEEDED WITH FUNCTION POINTERS
  /*int *array;
  list_t *list;
  bst_t *tree;
  */
  int index = 0;
  
    //checking the arguments after minpow/maxpow/size and setting booleans accordingly
    for(int i =3; i< argc;i++){ //NOW: adds the correlating pre-made function pointer construct to the real_funcs array
        if(strcmp(argv[i], "bt") == 0 && !bt){ 
          bt = 1;    
          realFuncs[index] = cfuncs[2];
          index++;         
        }
        else if(strcmp(argv[i],"ll") == 0 && !ll){
            ll =1;    
          realFuncs[index] = cfuncs[1];
          index++;
          
        } 
        else if(strcmp(argv[i], "ba") == 0 && !ba) {
          ba = 1; 
          realFuncs[index] = cfuncs[3];
          index++;
          
        }
        else if(strcmp(argv[i], "la") == 0 && !la){
            la = 1;      
          realFuncs[index] = cfuncs[0];
          index++;
          
        } 
 
    }
    //checks if no booleans where set, then all are true
    if(index == 0){
      index =4;
      ba = 1;
      la = 1;
      ll = 1;
      bt = 1;
      for(int i =0; i<4; i++){ //if no booleans are set, adds all function pointers structs to realFuncs
        realFuncs[i] = cfuncs[i];
      }
    }

  //Prints out header for table
  printf("%8s%9s","SIZE", "NSEARCH");
  if(la) printf("%15s","array");
  if(ll) printf("%15s","list");
  if(ba) printf("%15s","binary");
  if(bt) printf("%15s","tree");

  printf("\n");
  //Sets up current size variable
  int cursize = 1;
  for(int i =1; i<=minpow; i++){
    cursize *= 2;
  }  
  //intilize variables for calculating time
  clock_t start, end;
  double time;
 

for(int i = minpow; i<=maxpow;i++){//all tests run max-min times

    int check = 0;                                    //variable for traversing funciton pointer array
     printf("%8d%9d",cursize , cursize * repeats * 2);//prints size and numofchecks
    while(realFuncs[check].name != NULL){             //traverses array
        void *temp = realFuncs[check].create(cursize);//creates the tree/list/array struct fur current index

        start = clock();                              //begins time
      for(int j =0; j<repeats; j++){                  //loops repeating times for each function
        for(int k =0; k<(cursize*2)-1; k++){          // does equal hits/misses for the construct
           realFuncs[check].func(temp,cursize,k);     //calls the method of the current index
          
        }
      }
      end = clock();                                  //end time
      time = (double)(end-start);                     //calculates total time
      printf("%15.4e",time);                          //prints results

      realFuncs[check].destroy(temp);                 //frees space malloced
      check++;                                        //continues to next index
    }
    cursize *= 2;                                     //continues to next size
    
    printf("\n");                                     //next line on timing table
      
}

/* OLD CODE BEORE FUNCTION POINTERS
for(int i = minpow; i<=maxpow;i++){// We are printing maxpow-minpow tests
    printf("%8d%9d",cursize , cursize * repeats * 2); //printing the current size & numchecks
    
    if(la ==1){//linked array

      array = make_evens_array(cursize);      //allocates space in memory

      start = clock();                        //timing starts
      for(int j =0; j<repeats; j++){          //completes function repeating amount of times
        for(int k =0; k<(cursize*2)-1; k++){  //checks for an equal number of hits/misses in the data structure
           
          linear_array_search(array, cursize,k);  //function call
        } //for hits/misses
      }//for repeats

      end = clock();                          //timing ends
      time = (double)(end-start);             //calculates time
      printf("%15.4e",time);                  //prints results

      free(array);                            //FREES up memory allocated

    }//if linkey array

    if(ll ==1){ //linked list
      list = make_evens_list(cursize);        //allocates space in memory

      start = clock();                        //timing starts
      for(int j =0; j<repeats; j++){          //completes function repeating amount of times
        for(int k =0; k<(cursize*2)-1; k++){  //checks for an equal number of hits/misses in the data structure

          linkedlist_search(list,cursize,k);  //function call
        }//for hits/misses
      }//for repeats

      end = clock();                          //timing ends
      time = (double)(end-start);             //calculates time
      printf("%15.4e",time);                  //prints results

      list_free(list);                        //FREES up memory allocated

    }//if linked list

    if(ba ==1 ){ //binary array
      array = make_evens_array(cursize);      //allocates space in memory

      start = clock();                        //timing starts
      for(int j =0; j<repeats; j++){          //completes function repeating amount of times
        for(int k =0; k<(cursize*2)-1; k++){  //checks for an equal number of hits/misses in the data structure  

          binary_array_search(array, cursize, k);   //function call
        }//for hits/misses
      }//for repeats
      
      end = clock();                          //timing ends
      time = (double)(end-start);             //calculates time
      printf("%15.4e",time);                  //prints results

      free(array);                            //FREES up memory allocated
    
    }//if binary array

    if(bt == 1){ //binary tree
      tree = make_evens_tree(cursize);        //allocates space in memory

      start = clock();                        //timing starts
      for(int j =0; j<repeats;j++){           //completes function repeating amount of times
          for(int k=0; k<(cursize*2)-1;k++){  //checks for an equal number of hits/misses in the data structure

            binary_tree_search(tree,cursize,k);//function call
          }//for hits/misses
      }//for repeats

      end = clock();                          //timing ends
      time = (double)(end-start);             //calculates time
      printf("%15.4e",time);                  //prints results

      bst_free(tree);                         //FREES up memory allocated

    }//if binary tree

    printf("\n");                            //new line for next line in table

    cursize *= 2;                            //increases current size by *2 for next iteration

  }//for all lines on table

*/
  return 0; //finish
}//main