#include <stdio.h>
#include <time.h>
#include <stdlib.h>
static
inline
void
increment(unsigned
char
t[4]) {
if
(t[0] == 0xFF) {
if
(t[1] == 0xFF) {
if
(t[2] == 0xFF) {
++t[3];
}
++t[2];
}
++t[1];
}
++t[0];
}
int
main() {
const
unsigned
int
final_limit = (unsigned
int
) -1;
const
unsigned
int
intermediate_limit = 0xABCDEF00;
{
unsigned
char
t[4] = {0, 0, 0, 0};
printf
(
"%02X %02X %02X %02X - Initial Values\n"
, t[3], t[2], t[1], t[0]);
const
clock_t
start =
clock
();
unsigned
int
i;
for
(i = 0; i < intermediate_limit; ++i) {
++(*((unsigned
int
*)&t));
}
printf
(
"%02X %02X %02X %02X - Intermediate Values\n"
, t[3], t[2], t[1], t[0]);
for
(; i < final_limit; ++i) {
++(*((unsigned
int
*)&t));
}
const
clock_t
end =
clock
();
const
float
seconds = (
float
) (end - start) / CLOCKS_PER_SEC;
printf
(
"%02X %02X %02X %02X - Final Values\n"
, t[3], t[2], t[1], t[0]);
printf
(
"Seconds elapsed %f\t by casting array of unsigned chars to an unsigned int\n"
, seconds);
}
{
unsigned
char
t[4] = {0, 0, 0, 0};
printf
(
"%02X %02X %02X %02X - Initial Values\n"
, t[3], t[2], t[1], t[0]);
const
clock_t
start =
clock
();
unsigned
int
i;
for
(i = 0; i < intermediate_limit; ++i) {
increment(t);
}
printf
(
"%02X %02X %02X %02X - Intermediate Values\n"
, t[3], t[2], t[1], t[0]);
for
(; i < final_limit; ++i) {
increment(t);
}
const
clock_t
end =
clock
();
const
float
seconds = (
float
) (end - start) / CLOCKS_PER_SEC;
printf
(
"%02X %02X %02X %02X - Final Values\n"
, t[3], t[2], t[1], t[0]);
printf
(
"Seconds elapsed %f\t by using various if statements to control overflow\n"
, seconds);
}
return
0;
}