# clock

## A peculiar way to get the biggest (max/maximum) value between two variables using bitwise operations3

Recently, we wanted to make a test and see how we could find the maximum value between two variables using bitwise operations.

We ended up with the following peculiar way to get the biggest value between two variables using bitwise operations

```r = a ^ ((a ^ b) & -(a < b));

```

The above formula has two modes:

1. When `a < b`
2. When `a >= b`

### When `a < b` then the formula will change as follows:

```r = a ^ ((a ^ b) & 0xFFFFFFFF);

```

As we all (should) know, when one of the operators on a bitwise `AND` operation is composed only from `1`s, then the result is whatever value the other operator was holding.
So, the formula then simplifies as follows:

```r = a ^ (a ^ b);

```

which is equal to

```r = b;

```

because we when we apply twice the same value using `XOR` on another value, we revert back to the original value (so the second `^a` nullifies the first `^a`)

### When `a >= b` then the formula will change as follows:

```r = a ^ ((a ^ b) & 0x00000000);

```

When one of the operators on a bitwise `AND` operation is composed only from `0`s, then the result is always `0` no matter what value the other operator was holding.
So, the formula then simplifies as follows:

```r = a ^ (0x00000000);

```

which is equal to

```r = a;

```

because when one of the operators in a `XOR` operation is only composed from `0`s then the result will be the value of the other operator, no matter what it was.

### Full example

Below you will find a full example that compares the execution speed of the two methods by executing each several thousands of time on the same random data.

```#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
{
const clock_t start = clock();

srand(10);
unsigned long int i;
unsigned int max = 0;
for (i = 0; i < 1000000000; i++) {
const int a = rand();
max = max < a ? a : max;
}
const clock_t end = clock();
const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
printf("Seconds elapsed %f\tIf statement. Overall max value = %u\n", seconds, max);
}

{
const clock_t start = clock();

srand(10);
unsigned long int i;
unsigned int max = 0;
for (i = 0; i < 1000000000; i++) {
const int a = rand();
max = a ^ ((a ^ max) & -(a < max));
}
const clock_t end = clock();
const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
printf("Seconds elapsed %f\tBitwise operation. Overall max value = %u\n", seconds, max);
}
return 0;
}

```

### Results

Our results show that using the traditional if statement with assignment is faster than using our formula as expected.
Which makes sense as there is an if statement in the formula as well and then additional operations to get the result, instead of just the assignment.

```Seconds elapsed 5.770000 If statement. Overall max value = 2147483647
Seconds elapsed 6.180000 Bitwise operation. Overall max value = 2147483647```

10 times bigger input

```Seconds elapsed 57.450001 If statement. Overall max value = 2147483647
Seconds elapsed 63.869999 Bitwise operation. Overall max value = 2147483647```

## C/C++: Comparing the performance of syslog vs printf

The following code tries to compare the performance of `syslog()` with the `printf()` command. printf_vs_syslog.c (compressed) (578 downloads)
On our machine, it appears that `syslog()` is faster than `printf()`.

To be as fair as possible, when the application was executing, we were monitoring the system logs as well, so that they will be printed on screen.
On `CentOS 7`, you can see the `syslog` in the file `/var/log/messages`.
The command we used was: `sudo tail -f /var/log/messages`

### Results:

```printf: Seconds elapsed 0.480000
syslog: Seconds elapsed 0.180000```

### Full source code for test:

```#include <stdio.h>
#include <syslog.h>
// #include <stdlib.h> is needed for the resolution of EXIT_SUCCESS
#include <stdlib.h>
// #include <time.h> is needed for the clock() function and the macro CLOCKS_PER_SEC
#include <time.h>
// #include <unistd.h> and #include <sys/types.h> are needed for the functions uid_t getuid(void); and uid_t geteuid(void);
//getuid() returns the real user ID of the calling process.
//geteuid() returns the effective user ID of the calling process.
//These functions are always successful.
#include <unistd.h>
#include <sys/types.h>

#define RANGE (100000)

int main()
{
{
const clock_t start = clock();

unsigned int i;
for (i = 0; i < RANGE; i++){
printf ("Program started by Real User %u (Effective User %u)\n", getuid(), geteuid());
}
printf("\n");

const clock_t end = clock();
const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
printf("printf: Seconds elapsed %f\n", seconds);
}
{
const clock_t start = clock();

openlog ("bytefreaks", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
unsigned int i;
for (i = 0; i < RANGE; i++){
syslog (LOG_NOTICE, "Program started by Real User %u (Effective User %u)", getuid(), geteuid());
}
closelog ();

const clock_t end = clock();
const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
printf("syslog: Seconds elapsed %f\n", seconds);

}
return EXIT_SUCCESS;
}

```

## C: Code to time execution with accuracy greater than a second

The following application computes the time needed for a process to finish using the method `clock()`.
The result of the application is the time in seconds as a floating number (where 1.0 = 1 second).
It provides greater accuracy than seconds as the estimation is done using processor time used by the program.

```#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>

int main()
{

/* clock_t clock(void)
The clock() function returns an approximation of processor time used by the program.
The value returned is the CPU time used so far as a clock_t,
to get the number of seconds used, divide by CLOCKS_PER_SEC.
On error it returns -1. */
const clock_t start = clock();

/* svoid srand(unsigned int __seed)
The srand() function sets its argument as the seed for a new sequence of pseudo-random
integers to be returned by rand(). These sequences are repeatable by calling srand() with the
same seed value.
If no seed value is provided, the rand() function is automatically seeded with a value of 1. */
/* time_t time(time_t *__timer)
time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
If the __timer variable is not NULL, the return value is also stored there. */
srand(time(NULL));
unsigned long i;
for (i = 0; i < 10000000; i++)
{
/* int rand(void)
The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive. */
rand();
}
const clock_t end = clock();

/* ISO/IEC 9899:1999 7.23.1: Components of time
The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is
the number per second of the value returned by the `clock' function. */
/* CAE XSH, Issue 4, Version 2: <time.h>
The value of CLOCKS_PER_SEC is required to be 1 million on all
XSI-conformant systems. */
const float seconds = (float) (end - start) / CLOCKS_PER_SEC;

printf("Seconds elapsed %f\n", seconds);
return 0;
}
```

## On-screen clock/count down/counter (version 3, Java)

We ported the original watch from AutoHotKey to Java.

It started being used in the 106th IEEE Region 8 Committee Meeting that was held at the Fairmont Monte Carlo in Monaco on 19-20 of March 2016.

Key features

• You can change the icon that appears in the task bar by placing an `gif `file with the name watch.gif in the `config` directory of the executable
• You can drag the watch to a more convenient place with the mouse
• You can move the watch using the arrow keys. Using the shift key it makes the movement more precise. Using the control key it makes the movement faster.
• Double clicking on the watch it will hide it
• Double clicking on the tray menu icon of the watch it will toggle it’s visibility
• From the menu on the task bar you can “Set it on top”, allow mouse to click through the clock, hide/show the gui
• You can set the color of the Font and the Color of the background using a color picker
• You can set the transparency of the GUI
• The tool has two modes, it can operate as a clock that shows time in 24 hour mode or AM/PM and can operate as a count down tool (timer)
• The timer mode allows you to pause (and continue), reset and stop the time, reset and continue the time (using lap) or zero the clock

Good willed feedback is always welcome