Saturday 30 May 2015

Everyone is mad about Mad Max

Me, I staying @ home, writing a new (small) project  in python and to chill out, I listen to a proper real life account of real life 'Mad Max' guys --- truly gory stories written in old fashioned, elegantly worded plain English; read beautifully by a man with a most sonorous and peaceful voice.



And, if you liked that, Mr. Roger Melin has read lots more Librivox books.  This book is next on my list. And this too.

Wednesday 27 May 2015

Put some colour in your C code

I like colours.

Colours make things easier to spot, especially if you have to look at a few metres of test output bumpf --- looking for something blue or pink is much quicker.

So, I made myself a little color.h file that lives in /usr/include/ where it is available anywhere I need some colour in my code.

You will probably notice the colour names --- I took the liberty of renaming 'green' to 'moss', 'bold green' to just 'green' and 'magenta' to 'purple' and so on.  It's less to type, more descriptive, and, also functions as a small cheat map to understand the weird names the fashionistas in your life use to describe colour shades.

You can find the source for color.h here, note that the test code is present in the file in a comment, ready to be copied into a *.c file.

Enjoy!

                                                 

Monday 25 May 2015

A fake switch

This post is about a trick from the Dalvik VM Internals video, which was mentioned in the Coursera Android course that is currently running. (maybe the next million years will be more exciting?)
                                         
The claim was that this is the one case where goto isn't evil around minute 33:31 and that it performs better than switch.  Well, does it?  And if so, by how much?

This was the example:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define DISPATCH()  \
    { goto *op_table[*((s)++) - 'a']; }

static void interp_old(const char* s)

   static void *op_table[] = { 
   &&op_a, &&op_b, &&op_c, &&op_d, &&op_e };
   DISPATCH();
   op_a: printf("Hell"); DISPATCH();
   op_b: printf("o "); DISPATCH();
   op_c: printf("wo"); DISPATCH();
   op_d: printf("rld!\n"); DISPATCH();
   op_e: return;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
            
Switch is pretty switched on, so let's see how this compares!

The code that does the comparison can be found here.  Results(on my ancient 32 bit Dell):

With gcc -g:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 
Using default value of 100000.  Usage: ./dispatch #
Sample size = 100000 chars * 100000 iterations. 
Dispatch method:                         0.002152 
Dispatch shuffled method:                0.002144
Goto switch method:                      0.002641
Goto switch method 2:                    0.002351
For loop Switch method:                  0.002667
For loop Switch shuffled method:         0.002660 
Recursive switch method:                 0.010798
Recursive shuffled Switch method:        0.006913
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

With gcc -O3:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Using default value of 100000.  Usage: ./dispatch #
Sample size = 100000 chars * 100000 iterations.
Dispatch method:                         0.001083
Dispatch shuffled method:                0.001108
Goto switch method:                      0.001338
Goto switch method 2:                    0.001381
For loop Switch method:                  0.001465
For loop Switch shuffled method:         0.001385
Recursive switch method:                 0.001437
Recursive shuffled Switch method:        0.001385
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

So, there is a small difference, but it's really not that big a deal, unless you really are pressed for time and space.

The ever useful gcc manual has an entry about fun with local labels which will tell you more. Still, building this by hand is tedious, even if emacs is your best friend.  

What if you have a huge array of a struct that maps variable A to variable B and also has an optional function pointer F?  What you need is a 'fake_switch' generator, here is my version.  It should be easily adjustable for your taste. 

Goto it ;-)

Ps.: For extra fun, try this C to Assembler translator.  I really like the hover feature they built, but another thing that would be useful is showing the size of the generated code.
                                                                                                                                                                                    

Thursday 21 May 2015

Today is... 2 Prairial CCXXIII

The French Republican Calendar episode of 'Revolutions' covers fairly much every aspect of user interface design and every potential pitfall and foible possible.

In fact, I think Mike's podcast is probably the TL;DR of most UI  (and application) design books that are out here (and far more fun too).

Enjoy!

Ps.: I'm not sure why the designer eventually was guillotined, but, I'm also not overly surprised ;-D

Monday 11 May 2015

Fontic Kharma

I should not have seen this bug never before...

$ ./bin/dfconvert get ../gbg_samples/headers.odt foo3.html
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
DFAttribute *attrs: 0x85c15c0 , Tag tags: 1682 , char *value: DejaVu Sans1 ERROR:  no_op should never be called. 
...

Sunday 10 May 2015

Programming is fun (when stuff compiles)

Went to a BBQ.  Seemed to be a training session for competitive eating :-)

Came home, got my project's blueprint working.  Got emacs to smoke'n'choke with an M-x indent-region -- no idea what it did there, but it needed 20 minutes to indent gbg_test.h.  Impressive.


This music definitely helped things along :-)

Tuesday 5 May 2015

How to grab every mp3 from a Feedburner page

My little Apple finally died[1] and the major loss was my collection of the entire History of Rome podcast, by Mike Duncan.  Oh noes!

Sadly, Feedburner doesn't provide a big zip file of all the episodes, which is understandable, since it would be huge.

I could hunt around for a browser extension that does a mass download, but this is quicker, safer and way more fun:

Save the above file to your 'history_of_rome' directory as 'hist.xml'

Preferably overnight(if your 'broadband' comes via the phone line...), in the bash shell do this:

$ grep -e "enclosure url=" hist.xml | sed -e 's/[^"]*"//' -e 's/".*//' | xargs wget

And hopefully, if all goes well, in the morning you'll have this:

FINISHED --2015-05-05 02:06:31--
Total wall clock time: 2h 26m 38s
Downloaded: 191 files, 2.0G in 2h 24m 49s (240 KB/s)

~/history_of_rome>

You say that this podcast is a little bit short for such a long story?  Well, there's lots more out there, but this is my second favourite series:



[1] And, after all this, I found that the Apple actually wasn't broken, but the USB port I plugged in for a recharge was.  ;-D

Monday 4 May 2015

Shellicious!


Shell Style Guide -- how to code bash scripts in style.

How to scramble eggs inside their shell, for a neat pocket omelette, now with industrial trick:




The expectations of life depend upon diligence; the mechanic that would perfect his work must first sharpen his tools. 
          Confucius