Search This Blog

Pages

Tuesday, October 25, 2011

A simple counter/timer code in Java

It is a very common requirement to measure time taken by a particular method/function or block of code. The following simple timer can come handy:


long startTime = System.currentTimeMillis();
// Code block or method/function to time.
long endTime = System.currentTimeMillis();
System.out.println("Time taken: " + (endTime - startTime) + " milliseconds");

The accuracy of results can be improved by using System.nanoTime().

Monday, October 10, 2011

How to execute HTTP command in Linux on command line

curl is a command line utility to execute commands in protocols like HTTP, FTP, etc on Linux.

Example: curl -k --user admin@abc.xyz.com:password "https://abc.xyz.com:8080/api/data"

where:

-k : disable SSL (when server doesn't enforce SSL authentication)
--user: authentication credentials of format "username:password"


How to print time taken in milli seconds by a command on UNIX

Just replace "" in one line script below with your command and it will output the time taken by the command in milli seconds

You can also manipulate the dividing factor (1000000 in command below) to get output with varying degrees of precision

START=$(date +%s%N); ; END=$(date +%s%N); DIFF=$(( $END - $START )); echo "It took $(( $DIFF / 1000000)) milli seconds"