Update /demos/benchmarks

This uses GOS functionalities to count ticks instead of hardware specific cycle counters.
develop
Joel Bodenmann 2021-08-18 19:17:04 +02:00
parent c57bd08379
commit b98a6c258c
1 changed files with 41 additions and 82 deletions

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2012, 2013, Joel Bodenmann aka Tectu <joel@unormal.org> * Copyright (c) 2012 - 2021, Joel Bodenmann aka Tectu <joel@ugfx.io>
* Copyright (c) 2012, 2013, Andrew Hannam aka inmarket * Copyright (c) 2012 - 2021, Andrew Hannam aka inmarket <inmarket@ugfx.io>
* *
* All rights reserved. * All rights reserved.
* *
@ -31,87 +31,33 @@
#include "string.h" #include "string.h"
#include "gfx.h" #include "gfx.h"
#define SCB_DEMCR (*(volatile unsigned *)0xE000EDFC) #define RESULT_STR_LENGTH 32
#define CPU_RESET_CYCLECOUNTER do { SCB_DEMCR = SCB_DEMCR | 0x01000000; \
DWT_CYCCNT = 0; \
DWT_CTRL = DWT_CTRL | 1 ; } while(0)
static int uitoa(unsigned int value, char * buf, int max) { void benchmark(void)
int n = 0; {
int i = 0;
unsigned int tmp = 0;
if (!buf)
return -3;
if (2 > max)
return -4;
i=1;
tmp = value;
if (0 > tmp) {
tmp *= -1;
i++;
}
for (;;) {
tmp /= 10;
if (0 >= tmp)
break;
i++;
}
if (i >= max) {
buf[0] = '?';
buf[1] = 0x0;
return 2;
}
n = i;
tmp = value;
if (0 > tmp) {
tmp *= -1;
}
buf[i--] = 0x0;
for (;;) {
buf[i--] = (tmp % 10) + '0';
tmp /= 10;
if (0 >= tmp) {
break;
}
}
if (-1 != i) {
buf[i--] = '-';
}
return n;
}
void benchmark(void) {
gU32 i, pixels, ms, pps;
char pps_str[25];
gCoord height, width, rx, ry, rcx, rcy; gCoord height, width, rx, ry, rcx, rcy;
gColor random_color; gColor random_color;
gCoord fontHeight;
gFont font; gFont font;
gdispSetOrientation(gOrientation90); // Prepare resources
width = gdispGetWidth(); width = gdispGetWidth();
height = gdispGetHeight(); height = gdispGetHeight();
font = gdispOpenFont("UI2 Double"); font = gdispOpenFont("*");
fontHeight = gdispGetFontMetric(font, gFontHeight);
// Show intro message
gdispDrawStringBox(0, 0, width, 30, "uGFX - Benchmark", font, GFX_WHITE, gJustifyCenter); gdispDrawStringBox(0, 0, width, 30, "uGFX - Benchmark", font, GFX_WHITE, gJustifyCenter);
font = gdispOpenFont("UI2");
gdispDrawStringBox(0, height/2, width, 30, "5000 random rectangles", font, GFX_WHITE, gJustifyCenter); gdispDrawStringBox(0, height/2, width, 30, "5000 random rectangles", font, GFX_WHITE, gJustifyCenter);
gfxSleepMilliseconds(3000); gfxSleepMilliseconds(3000);
/* seed for the rand() */ // Seed RNG
srand(DWT_CYCCNT); srand(0);
pixels = 0;
CPU_RESET_CYCLECOUNTER; // Render rectangles and count ticks & pixels
gU64 pixels = 0;
for (i = 0; i < 5000; i++) { const gTicks ticksStart = gfxSystemTicks();
for (gU32 i = 0; i < 5000; i++) {
random_color = (rand() % 65535); random_color = (rand() % 65535);
rx = (rand() % (width-10)); rx = (rand() % (width-10));
ry = (rand() % (height-10)); ry = (rand() % (height-10));
@ -121,19 +67,33 @@ void benchmark(void) {
gdispFillArea(rx, ry, rcx, rcy, random_color); gdispFillArea(rx, ry, rcx, rcy, random_color);
pixels += (rcx+1)*(rcy+1); pixels += (rcx+1)*(rcy+1);
} }
const gTicks ticksEnd = gfxSystemTicks();
// Calculate result
char str_ticks[RESULT_STR_LENGTH];
char str_seconds[RESULT_STR_LENGTH];
char str_pps[RESULT_STR_LENGTH];
{
// Figure out how many ticks are 1 second
const gTicks ticksPerSecond = gfxMillisecondsToTicks(1000);
const gTicks ticksElapsed = ticksEnd - ticksStart;
const float secondsElapsed = (float)ticksElapsed / (float)ticksPerSecond;
gU32 pps = (float)pixels / secondsElapsed;
ms = DWT_CYCCNT / 168000; // Produce strings
pps = (float)pixels/((float)ms/1000.0f); memset(str_ticks, 0, RESULT_STR_LENGTH);
memset(str_seconds, 0, RESULT_STR_LENGTH);
memset(str_pps, 0, RESULT_STR_LENGTH);
snprintg(str_ticks, RESULT_STR_LENGTH, "%d ticks", ticksElapsed);
snprintg(str_pps, RESULT_STR_LENGTH, "%d pixels/s", pps);
}
memset (pps_str, 0, sizeof(pps_str)); // Show result
uitoa(pps, pps_str, sizeof(pps_str));
strcat(pps_str, " Pixels/s");
font = gdispOpenFont("UI2 Double");
gdispClear(GFX_BLACK); gdispClear(GFX_BLACK);
gdispDrawStringBox(0, 0, width, 30, "ChibiOS/GFX - Benchmark", font, GFX_WHITE, gJustifyCenter); gdispDrawStringBox(0, 0, width, 30, "uGFX - Benchmark", font, GFX_WHITE, gJustifyCenter);
gdispDrawStringBox(0, height/2, width, 30, pps_str, font, GFX_WHITE, gJustifyCenter); gdispDrawStringBox(0, height/2+0*(fontHeight+10), width, 30, str_ticks, font, GFX_WHITE, gJustifyCenter);
//gdispDrawString(20, height/2, pps_str, font, GFX_WHITE); gdispDrawStringBox(0, height/2+1*(fontHeight+10), width, 30, str_pps, font, GFX_WHITE, gJustifyCenter);
} }
int main(void) { int main(void) {
@ -141,9 +101,8 @@ int main(void) {
benchmark(); benchmark();
while(1) { while (gTrue)
gfxSleepMilliseconds(500); gfxSleepMilliseconds(500);
}
return 0; return 0;
} }