Created
April 12, 2018 10:09
-
-
Save borisfaure/f31a1836f4d384225d9feb615ea612a1 to your computer and use it in GitHub Desktop.
Benchmark for terminal apps
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 <stdlib.h> | |
#include <sys/time.h> | |
#define CLS "\e[2J" | |
#define COUNT 1000 | |
int w,h,mode; | |
long double tv1,tvg; | |
struct lt { | |
long double t; | |
char buf[1024]; | |
int div; | |
} ; | |
struct lt l[64]; | |
int li=0; | |
void g_begin() | |
{ | |
struct timeval tv; | |
gettimeofday(&tv,NULL); | |
tvg=((long double)tv.tv_sec)+ | |
((long double)(tv.tv_usec)/1000000.); | |
} | |
void g_end() | |
{ | |
struct timeval tv; | |
gettimeofday(&tv,NULL); | |
printf("Total : %Lg �s\n",((long double)tv.tv_sec)+((long double)(tv.tv_usec)/1000000.) - tvg); | |
} | |
void t_begin() | |
{ | |
struct timeval tv; | |
gettimeofday(&tv,NULL); | |
tv1=((long double)tv.tv_sec)+ | |
((long double)(tv.tv_usec)/1000000.); | |
} | |
long double t_end() | |
{ | |
struct timeval tv; | |
gettimeofday(&tv,NULL); | |
return ((long double)tv.tv_sec)+((long double)(tv.tv_usec)/1000000.) - tv1; | |
} | |
void push_result(char *strmode, char *str, int div) | |
{ | |
l[li].t=t_end(); | |
l[li].div=div; | |
snprintf(l[li].buf,1024,"%s: %s",strmode,str); | |
li++; | |
} | |
void print_result() | |
{ | |
int i; | |
for ( i=0 ; i<li ; i++ ) | |
{ | |
printf("%s: %#6Lg �s\n",l[i].buf,l[i].t*1000000/l[i].div); | |
} | |
} | |
static inline void gotoXY(int x, int y) | |
{ | |
printf("\e[%d;%dH",y,x); | |
} | |
static inline void scrollUP() | |
{ | |
printf("\eM"); | |
} | |
static inline void scrollDOWN() | |
{ | |
printf("\eD"); | |
} | |
void color() | |
{ | |
switch(mode) | |
{ | |
case 0: | |
break; | |
case 1: // rand-color-1 | |
printf("\e[%dm",(rand()%10)+30); | |
break; | |
case 2: // rand-color-2 | |
printf("\e[%d;%dm",(rand()%10)+30,(rand()%10)+40); | |
break; | |
case 3: // rand-color-3 | |
printf("\e[%d;%d;%dm",(rand()%10)+30,(rand()%10)+40,(rand()%10)+0); | |
break; | |
default: // mono | |
printf("\e[0;37;40m"); | |
} | |
} | |
void bench_gotoXY() | |
{ | |
gotoXY(rand()%(w+1),rand()%(h+1)); | |
color(mode); | |
putc((rand()%26)+65,stdout); | |
} | |
void bench_gotoYC() | |
{ | |
int i; | |
gotoXY(0,rand()%(h+1)); | |
for (i=0; i<w;i++ ) | |
{ | |
color(mode); | |
putc((rand()%26)+65,stdout); | |
} | |
} | |
void bench_gotoYL() | |
{ | |
int i; | |
char buf[w+1]; | |
gotoXY(0,rand()%(h+1)); | |
color(mode); | |
for (i=0; i<w;i++ ) | |
buf[i]=(rand()%26)+65; | |
buf[w]='\0'; | |
printf("%s",buf); | |
} | |
void bench_scrollUP() | |
{ | |
int i; | |
gotoXY(0,0); | |
scrollUP(); | |
for (i=0; i<w;i++ ) | |
{ | |
color(mode); | |
putc((rand()%26)+65,stdout); | |
} | |
} | |
void bench_scrollDOWN() | |
{ | |
int i; | |
gotoXY(0,h); | |
scrollDOWN(); | |
for (i=0; i<w;i++ ) | |
{ | |
color(mode); | |
putc((rand()%26)+65,stdout); | |
} | |
} | |
int main ( int argc, char **argv) | |
{ | |
int i; | |
char *strmode[4]={"monochrome ", | |
"1 color attr ", | |
"2 color attrs ", | |
"3 color attrs "}; | |
w=atoi(argv[1]); | |
h=atoi(argv[2]); | |
g_begin(); | |
for ( mode=0 ; mode < 4; mode++ ) | |
{ | |
printf(CLS); t_begin(); | |
for ( i=0; i<COUNT; i++ ) | |
bench_gotoXY(); | |
push_result(strmode[mode],"goto (X,Y), put a char ",COUNT); | |
printf(CLS); t_begin(); | |
for ( i=0; i<COUNT; i++ ) | |
bench_gotoYC(); | |
push_result(strmode[mode],"goto (Y,0), put W chars ",COUNT); | |
printf(CLS); t_begin(); | |
for ( i=0; i<COUNT; i++ ) | |
bench_gotoYL(); | |
push_result(strmode[mode],"goto (Y,0), put a line ",COUNT); | |
printf(CLS); t_begin(); | |
for ( i=0; i<COUNT; i++ ) | |
bench_scrollUP(); | |
push_result(strmode[mode],"goto (0,0), put W chars, scrollUP ",COUNT); | |
printf(CLS); t_begin(); | |
for ( i=0; i<COUNT; i++ ) | |
bench_scrollDOWN(); | |
push_result(strmode[mode],"goto (0,H), put W chars, scrollDOWN ",COUNT); | |
printf(CLS); | |
} | |
color(-1); | |
printf(CLS "\n"); | |
printf("mode : action : time / action\n"); | |
print_result(); | |
g_end(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment