Tuesday, November 29, 2011

Convert UNIX timestamp to human date using command-line

I'm working on a project where date/time are sent in UNIX timestamp. Sometimes when I'm debugging, I want to check whether the usually 10-digit number is correct.

The easiest way I found is using the command-line terminal:


~$ date -r 1322532029 
Tue Nov 29 10:00:29 SGT 2011

Friday, November 4, 2011

List of Inlined/intrinsic methods in Android's Dalvik VM

Android's Designing for Performance document, states:
In addition to all the usual reasons to prefer library code over rolling your own, bear in mind that the system is at liberty to replace calls to library methods with hand-coded assembler, which may be better than the best code the JIT can produce for the equivalent Java. The typical example here is String.indexOf and friends, which Dalvik replaces with an inlined intrinsic. Similarly, the System.arraycopy method is about 9x faster than a hand-coded loop on a Nexus One with the JIT.
Unfortunately, as are other Android SDK documentation, this doesn't tell you much which methods gets inlined with machine code.

Examining the Dalvik VM source code, I found dalvik/vm/InlineNative.c that starts with an introduction:


/*
 * Inlined native functions.  These definitions replace interpreted or
 * native implementations at runtime; "intrinsic" might be a better word.
 */


So the inlined methods are:
  • class String:
    • charAt(int)
    • compareTo(String)
    • equals(Object)
    • fastIndexOf(int, int)
    • isEmpty()
    • length()
  • class Math:
    • abs(int)
    • abs(long)
    • abs(float)
    • abs(double)
    • min(int, int)
    • max(int, int)
    • sqrt(double)
    • cos(double)
    • sin(double)
  • class Float:
    • floatToIntBits(float)
    • floatToRawIntBits(float)
    • intBitsToFloat(int)
  • class Double:
    • doubleToLongBits(double)
    • doubleToRawLongBits(double)
    • longBitsToDouble(long)
That's it! Now don't be so scared to use those methods in your code.

Bonus: fastIndexOf ? What is that? Java's String has indexOf, but never an fastIndexOf.

Answer: The implementation of String's indexOf(int) or indexOf(int, int) that Android uses, calls fastIndexOf if the char to be searched is <= 0xffff (which most probably is). So, if your code has a call of indexOf('x'), the native implementation will still be used.