Showing posts with label books. Show all posts
Showing posts with label books. Show all posts

Monday, 9 March 2015

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

tldr: 

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).

[1]
#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);

  exit(EXIT_SUCCESS);
}

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
   (some_function_a).
   (some_function_b).

* path/to/modifed-file2.c
   (some_function_c).
   (some_function_d).

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.

Sunday, 3 August 2014

Remedial Reading for the 'C'onfused.

Programming from the Ground up is an easy going, fun read, and goes a long way towards explaining all those inconvenient miracles that occur when you first try to use pointers in C.




... and, for some reason, Holger Czukays' short video of 'Ode To Perfume' nicely matches the topic (and it's pretty good hacking music).  The longer version of that song is here

(and if this reminds you of Stockhausen's work, well, there's a reason for that)

Update: This here is also a useful read about C's peculiarities.

Friday, 3 January 2014

January's Readinglist

People have been sending me great links to read!

Team Geek --- if you work with other people, this book is for you.

Paul Graham's essays --- even if you're not into business, those are a great read. 

Sleepyfox's talks --- Check out the code katas, get into pair programming and remember: couples that code together, stay together :)




Thursday, 12 December 2013

SVN Book diff idea

It would be nice to have SVN book versions on the website that have a differing portions of the text in blue for changes, and green for new additions and red for now obsolete parts.

It would make re-reading the book for a new version much easier for old hands, you could immediately spot everything you need to reconsider.

So, if I upgrade from 1.6 to 1.7, I would like to have the normal 1.7 book, the 1.7/1.6 'blue/green' version and the 1.6/1.7 'red' version.

(blue & green might not be possible to automate, but even just the blue version would be very useful, or perhaps it's even possible to have this all in one version.)

Friday, 6 September 2013

Productive Procrastination

I rediscovered my old math grind bible -- Engineering Mathematics by K. Stroud.

It's not a math course as such, more like a collection of sure-fire 'how do I calculate ...' recipes and a decent amount of exercises for every topic with answers.


So, I'm working through this book instead of playing Sudoku every time need a short puzzle break --- of course it's still procrastination, but, it feels a lot more productive!

Friday, 29 March 2013

My Reading List

Producing Open Software (2006)

A useful road map for anyone who wants to join or found a FOSS project.  It explains the social, organizational and legal issues that can arise.  Anyone needing to manage people will learn a lot from this.

Advanced Programming in the Unix environment (∞)

This is a pleasant, easy read, at least it starts that way, I'm on page 90, and it will probably get tougher.  It will teach you everything you always thought you knew about Unix, properly.  Read it all :)

The Art of Unix Programming (2003)

All about the culture, history and design philosophy of Unix, plus lots of tips and tricks. 

Object-Oriented Progamming in ANSI-C(1993)

I have to admit something.  I underestimated C.  26 keywords, I coded in C or similiar languages for years, so... what could possibly go wrong?  Then I saw OO C, and it could not be unseen.  This book helped me understand what is going on!

How to write shared Libraries (2011)

This will give you a framework and an appreciation of what you're dealing with when you play with libtool, or work with a project that uses shared libraries.

C Traps and Pitfalls (C-89)

Bugs!  You thought your bug story was bad?  Read these and learn (or just admire the problems from a safe distance...)

Unix Power Tools (2003)

This book was a life-saver so far, if you work through this, you're equipped for starting to study in earnest.