Upon further investigation, difftime can be left as is with no further optimization. Any optimization that *can* be done will have minimal effect in execution time. I will go over why that is.

double __difftime (time_t time1, time_t time0) { /* Convert to double and then subtract if no double-rounding error could result. */ if (TYPE_BITS (time_t) <= DBL_MANT_DIG || (TYPE_FLOATING (time_t) && sizeof (time_t) < sizeof (long double))) return (double) time1 - (double) time0; /* Likewise for long double. */ if (TYPE_BITS (time_t) <= LDBL_MANT_DIG || TYPE_FLOATING (time_t)) return (long double) time1 - (long double) time0; /* Subtract the smaller integer from the larger, convert the difference to double, and then negate if needed. */ return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0); }

For the **first if condition**, `TYPE_BITS (time_t)`

and `DBL_MANT_DIG`

are both constants, so the pre-processor will compare them at compile time and strip them from the executable altogether if they evaluate to true. The same applies to the **second if condition**. `TYPES_BITS`

<= `LDBL_MANT_DIG`

will be evaluated at compile time.

We can further validate this by compiling the code and looking at the assembly file:

I wrote a tester file that uses `time.h`

's `difftime.c`

:

// len_difftime_test.c #include <stdio.h> #include <time.h> #include <limits.h> #include <stdint.h> int main(){ // test time_t to uint_max conversion time_t time1 = time(NULL); time_t time0 = time(NULL) + 10; uintmax_t dt = (uintmax_t) time1 - (uintmax_t) time0; double delta = dt; printf("time1 = %d\ntime0 = %d\n", time1, time0); printf("(uintmax_t) time1 = %d\n", time1); printf("(uintmax_t) time0 = %d\n", time0); // test difftime function double result; result = difftime(time1, time0); printf("difftime(time1, time0) = %f\n", result); result = difftime(time0, time1); printf("difftime(time0, time1) = %f\n", result); return 0; }

Compile:

`gcc -g -o len_difftime_test len_difftime_test.c`

I use gdb debugger to get to line 18 which makes the first call to `difftime`

.

`gdb len_difftime_test`

Set a breakpoint at line 18 and run:

(gdb) b 18 Breakpoint 1 at 0x400638: file len_difftime_test.c, line 18. (gdb) r Starting program: /home/lisac/SourceCode/Seneca/spo600/project/src/glibc/time/len_difftime_test time1 = 1490051018 time0 = 1490051028 (uintmax_t) time1 = 1490051018 (uintmax_t) time0 = 1490051028 Breakpoint 1, main () at len_difftime_test.c:18 18 result = difftime(time1, time0); Step into the difftime function: __difftime (time1=1490051390, time0=1490051400) at difftime.c:103 103 { (gdb) s 114 return (long double) time1 - (long double) time0; (gdb) s 120 }

Short circuiting or test-reordering will not improve the executable since the pre-processor will rid of the comparison of *constants* when they evaluate to true. As we can see on *line 17*, there is no condition, only the returning subtract calculation.

Here is the pre-processor output:

`cpp difftime.c`

if ((sizeof (time_t) * 8) <= 53 <-- removed || (((time_t) 0.5 == 0.5) && sizeof (time_t) < sizeof (long double))) <-- removed return (double) time1 - (double) time0; if ((sizeof (time_t) * 8) <= 64 || ((time_t) 0.5 == 0.5)) <-- removed return (long double) time1 - (long double) time0;

Now I will be looking into more functions that are better candidates for optimization.