Skip to content

Instantly share code, notes, and snippets.

@larshei
Last active May 17, 2020 01:05
Show Gist options
  • Select an option

  • Save larshei/d7c70b4de939cedbed3ecb6df2ff6b23 to your computer and use it in GitHub Desktop.

Select an option

Save larshei/d7c70b4de939cedbed3ecb6df2ff6b23 to your computer and use it in GitHub Desktop.
This is just to give you an idea of the times required to perform the same action in different ways.
// compare times rquired for multiply and divide as well as pointer and array index addressing.
// e.g. from a small port to an STM32F4:
// Required times to scale the 1000 values to max=1.0 for 8192 times:
// Multiply, pointers: 14400
// Multiply, index: 15938
// Divide , pointers: 21077
// Divide , index: 19530
// The difference is actually a lot smaller than I expected.
#include <time.h>
#include <stdlib.h>
#define VALUE_COUNT 1000
#define FUNC_EXEC_COUNT 100000
int integer_values[VALUE_COUNT];
float scaled_values[VALUE_COUNT];
float factor;
float divider;
int execution_time(void (*func)(void), int execution_count) {
long start_clock, end_clock, diff_clock;
int result;
execution_count >> 2;
start_clock = clock();
for (int i = 0; i < execution_count ; i++) {
func();
func();
func();
func();
}
end_clock = clock();
diff_clock = end_clock - start_clock;
int duration = (int)diff_clock;
return duration;
}
void multiply_ptr() {
float* dest = scaled_values;
int* src = integer_values;
for (int i = 0 ; i < VALUE_COUNT ; i++) {
*dest++ = factor * *src++;
}
}
void multiply_index() {
for (int i = 0 ; i < VALUE_COUNT ; i++) {
scaled_values[i] = factor * integer_values[i];
}
}
void divide_ptr() {
float* dest = scaled_values;
int* src = integer_values;
for (int i = 0 ; i < VALUE_COUNT ; i++) {
*dest++ = (float)*src++ / divider;
}
}
void divide_index() {
for (int i = 0 ; i < VALUE_COUNT ; i++) {
scaled_values[i] = (float)integer_values[i] / divider;
}
}
void print_scaled_array() {
for (int i = 0 ; i < VALUE_COUNT ; i++) {
printf("%6.3f", scaled_values[i]);
if ((i + 1)%10 == 0) printf("\n");
}
printf("\n");
}
int main(void) {
time_t t_seed;
srand((unsigned) time(&t_seed) );
int max_value = 1000 + rand() % 1000;
int value;
for (int i = 0 ; i < VALUE_COUNT ; i++) {
value = rand() % max_value;
if (value > max_value) max_value = value;
integer_values[i] = value;
}
factor = 1.0 / max_value;
divider = max_value;
int mul_ptr_time = execution_time(multiply_ptr , FUNC_EXEC_COUNT);
int mul_ind_time = execution_time(multiply_index, FUNC_EXEC_COUNT);
int div_ptr_time = execution_time(divide_ptr , FUNC_EXEC_COUNT);
int div_ind_time = execution_time(divide_index , FUNC_EXEC_COUNT);
printf("Required times to scale the %d values to max=1.0 for %d times:\n", \
VALUE_COUNT, \
FUNC_EXEC_COUNT);
printf("Multiply, pointers: %d\n", mul_ptr_time);
printf("Multiply, index: %d\n", mul_ind_time);
printf("Divide , pointers: %d\n", div_ptr_time);
printf("Divide , index: %d\n", div_ind_time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment