Skip to content

Instantly share code, notes, and snippets.

@xyproto
Last active October 21, 2024 11:34
Show Gist options
  • Save xyproto/1b4c87c08b50799fb2a826a8abf2c884 to your computer and use it in GitHub Desktop.
Save xyproto/1b4c87c08b50799fb2a826a8abf2c884 to your computer and use it in GitHub Desktop.
// Simple C benchmark
#include <stdio.h>
#include <time.h>
inline int parse_bin_digit(const int ch)
{
switch (ch) {
case '0':
return 0;
case '1':
return 1;
default:
return -1;
}
}
inline int parse_bin_digit2(const int ch)
{
return (ch < '0' || ch > '1') ? -1 : ch - '0';
}
inline int parse_bin_digit3(const int ch)
{
if (ch == '0') {
return 0;
}
if (ch == '1') {
return 1;
}
return -1;
}
inline int parse_bin_digit4(const int ch)
{
if (ch == '0') {
return 0;
}
return (ch == '1' ? 1 : -1);
}
int main()
{
const int N = 100000000;
int sum = 0;
clock_t start, end;
double cpu_time_used;
// Prepare test data
char test_data[] = {'0', '1', '2', 'a', '0', '1', '1', '0'};
int data_size = sizeof(test_data)/sizeof(test_data[0]);
// Benchmark parse_bin_digit
sum = 0;
start = clock();
for (int i = 0; i < N; i++) {
sum += parse_bin_digit(test_data[i % data_size]);
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("parse_bin_digit time: %f seconds, sum=%d\n", cpu_time_used, sum);
// Benchmark parse_bin_digit2
sum = 0;
start = clock();
for (int i = 0; i < N; i++) {
sum += parse_bin_digit2(test_data[i % data_size]);
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("parse_bin_digit2 time: %f seconds, sum=%d\n", cpu_time_used, sum);
// Benchmark parse_bin_digit3
sum = 0;
start = clock();
for (int i = 0; i < N; i++) {
sum += parse_bin_digit3(test_data[i % data_size]);
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("parse_bin_digit3 time: %f seconds, sum=%d\n", cpu_time_used, sum);
// Benchmark parse_bin_digit4
sum = 0;
start = clock();
for (int i = 0; i < N; i++) {
sum += parse_bin_digit4(test_data[i % data_size]);
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("parse_bin_digit4 time: %f seconds, sum=%d\n", cpu_time_used, sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment