Last active
November 30, 2016 21:27
-
-
Save jlgarridol/109f33eeee8532f3424571235e2e8603 to your computer and use it in GitHub Desktop.
Multipurpose library with a wide range of useful functions for C language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This file is source code of jkalib | |
jkalib (C) 2016-2017 JKA Network <contacto@jkanetwork.com> | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
jkalib is a multipurpose library with a wide range of useful functions for C language | |
Some code is from https://davidcapello.com/blog/cpp/medir-el-tiempo-de-una-rutina/ | |
*/ | |
//Standard libraries | |
#include <stdio.h> | |
#include <time.h> | |
#ifdef __linux__ | |
#include <sys/time.h> | |
typedef struct timeval event ; | |
#else | |
#include <windows.h> | |
typedef LARGE_INTEGER event; | |
#endif | |
//Prototypes | |
void pause(void); | |
int clean_stdin(void); | |
void printError(char[]); | |
double eventDiff(event *,event*); | |
void setEvent(event *); | |
/** | |
Description: Stop the execution until the user push one key | |
IN/ VOID | |
/OUT VOID | |
*/ | |
void pause(void){ | |
clean_stdin(); | |
getchar(); | |
} | |
/** | |
Description: Clean the keyboard buffer | |
IN/ VOID | |
/OUT 1 | |
*/ | |
int clean_stdin(void){ | |
while(getchar()!='\n'); | |
return 1; | |
} | |
/** | |
Description: calculate a difference into two events | |
IN/ first event, last event (timeval) | |
/OUT difference into last and first event in milliseconds | |
*/ | |
double eventDiff(event *first,event *last){ | |
#ifdef __linux__ | |
return | |
((double)(last->tv_sec + (double)last->tv_usec/1000000)- | |
(double)(first->tv_sec + (double)first->tv_usec/1000000))*1000.0; | |
#else | |
event freq; | |
QueryPerformanceFrequency(&freq); | |
return ((double)(last->QuadPart - first->QuadPart) / (double)freq.QuadPart) * 1000.0; | |
#endif | |
} | |
/** | |
Description: return an event | |
IN/ event pointer | |
/OUT VOID | |
*/ | |
void setEvent(event *ev){ | |
#ifdef __linux__ | |
gettimeofday(ev,NULL); | |
#else | |
QueryPerformanceCounter(ev); | |
#endif | |
} | |
/** | |
Description: print an error in stderr | |
IN/ String whith the mesage | |
/OUT VOID (In stderr the mesage) | |
*/ | |
void printError(char mesage[]){ | |
fprintf(stderr,mesage); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "jka.h" | |
int main(int argc, char const *argv[]) | |
{ | |
event init,end; | |
double secs; | |
int i; | |
setEvent(&init); | |
for(i=0;i<50000;i++){ | |
printf("%d\n",i); | |
} | |
setEvent(&end); | |
secs = eventDiff(&init,&end); | |
printf("\n%.16f milliseconds\n", secs); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment