C — How to check how much time a section of code takes to execute.

This requires use of standard C library support for the clock_gettime() function. This function returns clock time as a data-structure (struct timespec) that includes the the clock time in seconds and nano-seconds.

Call this function one time before the section of code to evaluate and once after and then take a difference of the end time versus the start time to figure out the time taken in the section of code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <time.h> // for the clock_* APIs and struct timespec

struct timespec timespec_diff(struct timespec end, struct timespec start)

{
    timespec temp;


    if ((end.tv_nsec-start.tv_nsec) < 0)
    { // If this is the case, then the seconds values are different by atleast 1.
        temp.tv_sec = end.tv_sec - start.tv_sec - 1; // Borrow one second from the seconds field...
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; //...and use it in the nano-second field.
    }
    else
    {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }

    return temp;
}

void testThis()
{
    int i;
    struct timespec start, end, diff;


    clock_gettime(CLOCK_REALTIME, &start);

    for (i = 0; i < 100000; i++)
    {
        x = i * 45;
    }

    clock_gettime(CLOCK_REALTIME, &end);

    diff = timespec_diff(end, start);
    printf("Section of code took %u seconds %d nano-seconds\n", diff.tv_sec, diff.tv_nsec);
}
Share

Firefox and FormData Objects — Gotcha/Issue

When sending over a file using FormData objects and XMLHttpRequest via POST, make sure not to name the file object with the string “file” as in

formData.append(“file”, e.target.files[0]);

Use any other name than “file”, e.g.:

formData.append(“userfile”, e.target.files[0]);

Not sure what happens under the hood, but for some reason with a PHP based server (perhaps the problem was with the framework being used), the POST never occurred until I changed the key string naming.

Share

Async Programming Patterns

Note To Self: In any system where you have multiple processes or tasks handling processing of various events and you provide a mechanism in one process or task for other modules to register a callback, you should also allow for a life-time parameter in the registration that is returned whenever the callback is called. This is so that if callbacks are processed asynchronously by queue-ing messages by the process registering callbacks, there can be stale messages in queue during which time the registration could be deleted and new registrations can happen. So there has to be a mechanism for the callback owner to be able to distinguish the life-time instance of the registration against which the callback was originally made.

Share

Windows 7 Wi-Fi slow start issue on startup with VirtualBox

UPDATE:

After repeatedly being exposed to this problem, I think I finally understand what the issue is with this slow-start — mostly conjecture on my part.

On my PC, I don’t have any wired Ethernet connection (I have Ethernet ports, but I haven’t connected them). I rely on the Wi-Fi connection for my Internet access. With the VirtualBox software installed, it installs a virtual Ethernet port on Windows side of things to connect with a Virtual LAN Bridge that networks the VirtualBox virtual machine with the Windows machine. This virtual Ethernet port comes up right away (I assume it is set to be always up) while, I think, the Wi-Fi port probably has some small initial start-up time that it takes to initialize itself. At start-up or wake-up, Windows, I think, then sees only one up network port (the port into the Virtual Bridge) and tries initially to use this port to connect to the Internet and continues to try and retry and finally, after some retries/timeouts,  falls back to using the Wi-Fi port for the Internet connection which by this time has come up.

Unfortunately, I haven’t figured out a clean solution that will allow me to keep both the VirtualBox installation as-is *and* give my Wi-Fi port some priority (or have some sort of Windows networking start up delay that allows the Wi-Fi port some time to initialize itself before Windows tries to start using it).

If anyone with VirtualBox experience knows of some way of tweaking the virtual-port to start-up after a delay, please chime in in the comments.

 

Original Blog Entry:

I had installed Oracle’s (previously Sun’s) VirtualBox software on my Windows 7 machine and subsequently I noticed that every time I started into Windows 7 after waking up from sleep or powering on the PC or rebooting the PC, the WiFi connection would not come up for 1-2 minutes. After a lot of experimentation, I found that un-checking the “VirtualBox Bridged Networking Driver” setting in the “Wireless Network Connection Properties” dialog for my wireless card solved the problem. No more slow-start. Not sure if this results in other problems for VirtualBox, but for now this is good for me. Getting to the dialog is via Control Panel > Network and Internet > Network and Sharing Center  and click on “Change Adapter Settings” on the left-hand side. This brings up the list of adapters and right-click on your wireless adapter and choose “Properties” for the “Wireless Network Connection Properties” for your wireless adapter.

Share

Are you unable to use C sizeof operator as expected in pre-processor macros?

If you are trying to use the C programming language’s sizeof operator in a C pre-processor macro and find that you are unable to use it as expected and you see compiler errors (actually C pre-processor (CPP) errors), you are really seeing expected behavior. The sizeof operator works only during compile time and the sizes of structures are only known at compile time and it is not an operator supported by the C pre-processor.

Share

Reduce size of scanned images saved as PDF files

I am new to the phenomenon of scanned documents using a color scanner being very large in size. An 8 page document that was about 105K when originally delivered to me as a PDF file ended up being about 12.5M after I had printed it out, signed my signature and scanned it to a PDF file again.

Looking around the web, it seemed like there were many suggestions on reducing the size of a scanned document. There are software that will compress the size of your scanned PDF file, but nothing free. Ultimately I played around with the settings of my scanner (which is a Canon MP620 Pixma printer/scanner/copier combo) and found that the following settings reduced the size of the scanned image drastically.

The single change that had the biggest impact was to change the scan settings so that the document was scanned as a Black & White image instead of a Color image. Another setting that reduced the size further was to use 150dpi when scanning instead of 300dpi which was set as the default. Finally a big reduction results when you choose high compression when converting to a PDF file. Finally I went with just the Black & White scan setting only (since changing the other settings also reduced the quality of the resulting PDF file) and I was able to get a PDF of about 2M instead of the 12.5M that I was originally getting. So a decent amount of reduction in size.

Share