Posts

Three Optimization Tips for C++ - by Andrei Alexandrescu

Image
This is an approximate transcript of my talk at Facebook NYC on December 4, 2012, which discusses optimization tips for C++ programs. The video of the talk is  here  and the accompanying slides are  here . Scope Commonly given advice about approaching optimization in general, and optimization of C++ code in particular, includes: Quoting Knuth more or less out of context The classic one-two punch: (a) Don't do it; (b) Don't do it yet Focus on algorithms, not on micro-optimization Most programs are I/O bound Avoid constructing objects unnecessarily Use C++11's rvalue references to implement move constructors That's great advice, save for two issues. First, it has becomed hackneyed by overuse and is often wielded to dogmatically smother new discussions before they even happen. Second, some of it is vague. For example, "choose the right algorithm" is vacuous without a good understanding of what algorithms are best supported by the computing fabri...

Retrieving IP address of each Interface through MAC address for a host machine

public static void main(String args[]) throws Exception { System.out.println(getIPFromMac("54DE06EA540A")); } /** * @param macAddress *            should be in byte format without non-numeric values *            eg:0123456789AB * @return * @throws SocketException */ static String getIPFromMac(String macAddress) throws SocketException { Enumeration netintfaces = java.net.NetworkInterface .getNetworkInterfaces(); HashMap macIpMapper = new HashMap (); while (netintfaces.hasMoreElements()) { try { NetworkInterface ninterfaces = netintfaces.nextElement(); String mac = DatatypeConverter.printHexBinary((ninterfaces .getHardwareAddress())); Enumeration netIpaddresses = ninterfaces .getInetAddresses(); while (netIpaddresses.hasMoreElements()) { try { InetAddress ip = netIpaddresses.nextElement(); String ipStr = ip.getHostAddress(); ...

Java Data Time Format made simple

/** * Format a time from a given format to given target format * * @param inputFormat * @param inputTimeStamp * @param outputFormat * @return * @throws ParseException */ private static String TimeStampConverter ( final String inputFormat , String inputTimeStamp , final String outputFormat ) throws ParseException { return new SimpleDateFormat ( outputFormat ). format ( new SimpleDateFormat ( inputFormat ). parse ( inputTimeStamp )); } Sample Usage is as Following: try { String inputTimeStamp = "Tue Feb 05 13:59:44 IST 2013" ; final String inputFormat = "EEE MMM dd HH:mm:ss z yyyy" ; final String outputFormat = "yyyy.MM.dd GGG hh:mm aaa" ; System . out . println ( TimeStampConverter ( inputFormat , inputTimeStamp , outputFormat )); } catch ( ParseException e ) {...

How and When, does finally block get executed in java - deep thinking from C++ concepts RAI Resource Allocation is Initializtion

Does finally block runs even after block/object/function get returned? Yes. Even if there was an  Exception  within  catch  block,  finally  will be executed. If you are familiar with C++, just think  finally  as the  destructor  of an  object . What ever the state of a statement within the object,  ~Destructor  will be executed. But you cant put  return  within finally [some compilers allow though]. See the code below: See how global variable  y  been changed. Also see how  Exception1  been covered by  Exception2 . using System ; using System . Collections . Generic ; using System . Linq ; using System . Text ; namespace finallyTest { class Program { static int y = 0 ; static int testFinally () { int x = 0 ; try { x = 1 ; throw new Exception ( "Exc...