Skip to content

Instantly share code, notes, and snippets.

@markprovan
Created November 24, 2011 21:02
Show Gist options
  • Select an option

  • Save markprovan/1392277 to your computer and use it in GitHub Desktop.

Select an option

Save markprovan/1392277 to your computer and use it in GitHub Desktop.
My C Shit
#define VERSION "P2V3_3 by Mark Provan. Updated last on 24/11/11\n"
#include <stdio.h>
#include <string.h>
#include <time.h>
static clock_t _startTick,_stopTick;
#define SNAP() (_startTick = clock())
#define SHOT() (_stopTick = clock())
#define ELAPSED() (_stopTick - _startTick)
#define BLOCKSIZE 10000000
char str1[BLOCKSIZE];
char str2[BLOCKSIZE];
void * mymemcpyv1( void * destination, const void * source, size_t num ) {
int i;
for ( i = 0 ; i < num ; ++i ) {
((char *)destination)[i] = ((char *)source)[i];
}
}
void * mymemcpyv2 ( void * destination, const void * source, size_t num ) {
int i;
for ( i = 0 ; i < num ; ++i ) {
*((char *)destination + i) = *((char *)source + i);
}
return (destination);
}
void *mymemcpyv3 ( void *destination, const void *source, size_t num ) {
char *dst = (char *) destination;
const char *src = (char *) source;
while (num--) {
*dst++ = *src++;
}
return (destination);
}
int main ()
{
printf(VERSION);
printf("\nOn this machine, CLOCKS_PER_SEC is %d\n", (int)CLOCKS_PER_SEC);
printf("This means the timing accuracy is %f seconds\n\n", 1.0/CLOCKS_PER_SEC);
printf("About to invoke a C memcopy of %d bytes\n", BLOCKSIZE);
SNAP();
memcpy(str2,str1, BLOCKSIZE);
SHOT();
double runTimeLib = (double)ELAPSED()/CLOCKS_PER_SEC;
printf("It took %f seconds to execute memcpy.\n", runTimeLib);
printf("...at a speed of %f Mb/s.\n\n", ((BLOCKSIZE/runTimeLib)/1024)/1024);
printf("Stage 2: About to invoke a mymemcpyv1 of %d bytes\n", BLOCKSIZE);
SNAP();
mymemcpyv1(str1, str2, BLOCKSIZE);
SHOT();
double myrunTime1 = (double)ELAPSED()/CLOCKS_PER_SEC;
printf("It took %f seconds to execute mymemcpy.\n", myrunTime1);
printf("...at a speed of %f Mb/s.\n\n", ((BLOCKSIZE/myrunTime1)/1024)/1024);
printf("Stage 3: About to invoke a mymemcpyv2 of %d bytes\n", BLOCKSIZE);
SNAP();
mymemcpyv2(str1, str2, BLOCKSIZE);
SHOT();
double myrunTime2 = (double)ELAPSED()/CLOCKS_PER_SEC;
printf("It took %f seconds to execute mymemcpy.\n", myrunTime2);
printf("...at a speed of %f Mb/s.\n\n", ((BLOCKSIZE/myrunTime2)/1024)/1024);
printf("Stage 4: About to invoke a mymemcpyv3 of %d bytes\n", BLOCKSIZE);
SNAP();
mymemcpyv3(str1, str2, BLOCKSIZE);
SHOT();
double myrunTime3 = (double)ELAPSED()/CLOCKS_PER_SEC;
printf("It took %f seconds to execute mymemcpy.\n", myrunTime3);
printf("...at a speed of %f Mb/s.\n\n\n", ((BLOCKSIZE/myrunTime3)/1024)/1024);
/*Print out data for easier analysis */
printf(" *****RESULTS****** \n\n");
printf("----------------------------------------------\n");
printf("| MEMCPY VERSION | RUNTIME (SECS) |\n");
printf("----------------------------------------------\n");
printf("| memcpy | %f |\n", runTimeLib);
printf("----------------------------------------------\n");
printf("| mymemcpyv1 | %f |\n", myrunTime1);
printf("----------------------------------------------\n");
printf("| mymemcpyv2 | %f |\n", myrunTime2);
printf("----------------------------------------------\n");
printf("| mymemcpyv3 | %f |\n", myrunTime3);
printf("----------------------------------------------\n\n");
/*Determine the fastest function */
double t[4] = {runTimeLib, myrunTime1, myrunTime2, myrunTime3};
double fastest = 1000.00;
int fastest_index = 0;
int i=0;
for (i=0; i<4; i++){
if (t[i] < fastest ) {
fastest = t[i];
fastest_index = i;
}
}
/* Assign the fastest function its respective name */
char fastest_name[11];
switch (fastest_index) {
case 0 : strncpy(fastest_name, "memcpy", 6);
fastest_name[6] = '\0';
break;
case 1 : strncpy(fastest_name, "mymemcpyv1", 10);
fastest_name[10] = '\0';
break;
case 2 : strncpy(fastest_name, "mymemcpyv2", 10);
fastest_name[10] = '\0';
break;
case 3 : strncpy(fastest_name, "mymemcpyv3", 10);
fastest_name[10] = '\0';
break;
}
/*Output fastest and terminate */
printf("The quickest implementation of memcopy is %s, completing in %f seconds\n\n",fastest_name, t[fastest_index]);
printf("Program Terminated\n\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment