Monday, 9 March 2015

... and it was such a great idea... :(


Lesson 1: You cannot cheat your way around strlen() by using sizeof(s)/sizeof(s[0]) because you cannot stuff a char *s into a char s[] for free.
Lesson 2: Don't use the clock mechanism in the proggy below, because it's unreliable on hyper-threaded CPUs.
Lesson 3: Don't use floats.

The full story:

I am currently reading the book Modern C.  In it, I came across the advice that you can compute the size of an array by doing  sizeof(s)/sizeof(s[0]).  Hmm, maybe I could use that instead of strlen, because surely this is much quicker!

So... hackety-hack, I made this[1] and got that output:

A: Time spent:           0.045580  count = 35
B: Time spent:           0.180489  count = 34

Then after some fiddling around, I realised that I of course cannot assign a char *s to a char s[] (without paying the full price), despite either form working in strlen().

I mailed my guru about this unsuccess for his entertainment (and that of his colleagues :-) , and the message coming back informed me that clock is considered harmful, and floats are also bad.  Being curious I asked why clock is dodgy (since time.h surely should be sound), and was informed that on a hyper-threaded CPU, it's unreliable, which of course is no good if you're trying to profile anything. Apparently it's better to use this little creation.

So, how to do find out if your cpu is hyper-threaded?  You do cat /proc/cpuinfo and look up the spec. (mine isn't ht but that's no excuse).

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>

int main (int argc, char *argv[])
  clock_t begin, end;
  double time_spent;

  // sadly I can't assign ss to s :(
  // char *ss = "supercalifragilisticexpialidocious";                                
  char s[] = "supercalifragilisticexpialidocious";
  size_t max = 10000000;
  int count;

  begin = clock();

  for (size_t i = 0; i < max; i++)
      count = sizeof(s)/sizeof(s[0]);

  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("A: Time spent:  \t %f  count = %d\n",time_spent, count);

  begin = clock();

  for (size_t i = 0; i < max; i++)
      count = strlen(s);

  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("B: Time spent:  \t %f  count = %d\n",time_spent, count);


Saturday, 7 March 2015

A little perl alias for git log messages

To get a nice log message for your git diff patch which makes it easy for a reviewer to check your stuff, try this little perl proggy:

alias gitdifflogmesg="perl -ne 'BEGIN {print \"[[[\";} /^\\+\\+\\+ b\\/(\\S+)/ && print \"\\n\\n* \$1\";  /^@\@.*[ *]([^ ]+)\(/ && print \"\\n  (\$1).\"; END { print \"\\n\\n]]]\\n\";}'"

Put this into your .bashrc (as one line), and, grab a patch you made with git diff, and use the alias like so:

cat your.patch | gitdifflogmesg

The output is something like this:

* path/to/modifed-file1.c

* path/to/modifed-file2.c

Now all you need to do is some accounting -- a good 50 char intro line and say what you did with all those functions and your log message is ready for sharing.  Of course not all log messages need this level of scrutiny, but, this goes a long way towards helping your reviewers and also, to help yourself to produce a great patch (or mailing list post).

Note that this does not pick up everything --- new #includes and new functions for example.  Then again, whilst that one liner is kinda cute, this service should really be offered by git itself.  But whilst git doesn't have this feature, this goes a long way towards making log messages easy ;-)

I am not a perl wizard though, I just read the book 'Perl One-Liners: 130 Programs That Get Things Done' -- even if you only read the first 3 chapters, you gain a handy little skill, plus, it's just fun to fiddle with.

Wednesday, 4 March 2015


I am proud to announce that I've been made a full committer and PPMC member of the Apache Incubator project Corinthia.

I'll be coding in C and Javascript, and I'll be looking to help with the editor and whatever else needs attention :-)