Skip to content

Instantly share code, notes, and snippets.

@TunaCici
Last active May 30, 2023 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TunaCici/9fc064a4c2bb47a8ad190dc6f6592133 to your computer and use it in GitHub Desktop.
Save TunaCici/9fc064a4c2bb47a8ad190dc6f6592133 to your computer and use it in GitHub Desktop.
Very basic C code that benchmarks both the 'stdio' and the 'printf' function. WARNING: This is an extremely simple benchmark. The results heavily depend on your 'terminal', 'stdio' and 'printf' implementation.
/*
* Author: Tuna Cici
* Date: 30 May 2023
*
* This file contains the benchmarking code for the 'printf' function.
* It is used to measure the time it takes to print a string to the 'stdout'.
*
*/
#if defined(_WIN32) || defined(_WIN64)
#define OS_Windows
#elif defined(__linux__)
#define OS_Linux
#elif defined(__APPLE__)
#define OS_MacOS
#endif
/* Standard Libraries (printf & uint32_t) */
#include <stdio.h>
#include <stdint.h>
/* Time Library (timer with 'ms' resolution) */
#ifdef OS_Windows
#include <Windows.h>
#include <sysinfoapi.h>
#elif defined(OS_Linux) || defined(OS_MacOS)
#include <time.h>
#endif
void get_current_time(uint32_t *time)
{
#ifdef OS_Windows
*time = GetTickCount();
#elif defined(OS_Linux) || defined(OS_MacOS)
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
*time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
#endif
}
void print_str(const char *str, uint32_t times)
{
for (uint32_t i = 0u; i < times; i++)
{
printf("%s", str);
}
}
int main(int argc, char const *argv[])
{
const uint32_t CASE_1_SIZE = 1000u;
const uint32_t CASE_2_SIZE = 10000u;
const uint32_t CASE_3_SIZE = 100000u;
const char STR_32[32] = "abcdefghijklmnopqrstuvwxyz12345";
const char STR_64[64] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
const char STR_128[128] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890__";
const char STR_256[256] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890__abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890___";
uint32_t str_32_case1_start = 0u, str_32_case1_end = 0u;
uint32_t str_32_case2_start = 0u, str_32_case2_end = 0u;
uint32_t str_32_case3_start = 0u, str_32_case3_end = 0u;
uint32_t str_64_case1_start = 0u, str_64_case1_end = 0u;
uint32_t str_64_case2_start = 0u, str_64_case2_end = 0u;
uint32_t str_64_case3_start = 0u, str_64_case3_end = 0u;
uint32_t str_128_case1_start = 0u, str_128_case1_end = 0u;
uint32_t str_128_case2_start = 0u, str_128_case2_end = 0u;
uint32_t str_128_case3_start = 0u, str_128_case3_end = 0u;
uint32_t str_256_case1_start = 0u, str_256_case1_end = 0u;
uint32_t str_256_case2_start = 0u, str_256_case2_end = 0u;
uint32_t str_256_case3_start = 0u, str_256_case3_end = 0u;
/* BEGIN STR_32 */
get_current_time(&str_32_case1_start);
print_str(STR_32, CASE_1_SIZE);
get_current_time(&str_32_case1_end);
get_current_time(&str_32_case2_start);
print_str(STR_32, CASE_2_SIZE);
get_current_time(&str_32_case2_end);
get_current_time(&str_32_case3_start);
print_str(STR_32, CASE_3_SIZE);
get_current_time(&str_32_case3_end);
/* END STR_32 */
/* BEGIN STR_64 */
get_current_time(&str_64_case1_start);
print_str(STR_64, CASE_1_SIZE);
get_current_time(&str_64_case1_end);
get_current_time(&str_64_case2_start);
print_str(STR_64, CASE_2_SIZE);
get_current_time(&str_64_case2_end);
get_current_time(&str_64_case3_start);
print_str(STR_64, CASE_3_SIZE);
get_current_time(&str_64_case3_end);
/* END STR_64 */
/* BEGIN STR_128 */
get_current_time(&str_128_case1_start);
print_str(STR_128, CASE_1_SIZE);
get_current_time(&str_128_case1_end);
get_current_time(&str_128_case2_start);
print_str(STR_128, CASE_2_SIZE);
get_current_time(&str_128_case2_end);
get_current_time(&str_128_case3_start);
print_str(STR_128, CASE_3_SIZE);
get_current_time(&str_128_case3_end);
/* END STR_128 */
/* BEGIN STR_256 */
get_current_time(&str_256_case1_start);
print_str(STR_256, CASE_1_SIZE);
get_current_time(&str_256_case1_end);
get_current_time(&str_256_case2_start);
print_str(STR_256, CASE_2_SIZE);
get_current_time(&str_256_case2_end);
get_current_time(&str_256_case3_start);
print_str(STR_256, CASE_3_SIZE);
get_current_time(&str_256_case3_end);
/* END STR_256 */
printf("\n\n");
printf("Case 1: Print %u times\n", CASE_1_SIZE);
printf("\tSize 32: %u ms | Size 64: %u ms | Size 128: %u ms | Size 256: %u ms\n",
str_32_case1_end - str_32_case1_start,
str_64_case1_end - str_64_case1_start,
str_128_case1_end - str_128_case1_start,
str_256_case1_end - str_256_case1_start);
printf("Case 2: Print %u times\n", CASE_2_SIZE);
printf("\tSize 32: %u ms | Size 64: %u ms | Size 128: %u ms | Size 256: %u ms\n",
str_32_case2_end - str_32_case2_start,
str_64_case2_end - str_64_case2_start,
str_128_case2_end - str_128_case2_start,
str_256_case2_end - str_256_case2_start);
printf("Case 3: Print %u times\n", CASE_3_SIZE);
printf("\tSize 32: %u ms | Size 64: %u ms | Size 128: %u ms | Size 256: %u ms\n",
str_32_case3_end - str_32_case3_start,
str_64_case3_end - str_64_case3_start,
str_128_case3_end - str_128_case3_start,
str_256_case3_end - str_256_case3_start);
return 0;
}
@TunaCici
Copy link
Author

TunaCici commented May 30, 2023

Simply compile with your choice of CC: clang -Wall printf_benchmark.c -o benchmark and run ./benchmark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment