Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 47 total

Code execution timer

These two bits of code can be used to record & output the execution time of a piece of code in microseconds. NOTE: The functions used here are only available on BSD-like systems (I think).
/* Put this line at the top of the file: */
#include <sys/time.h>

/* Put this right before the code you want to time: */
struct timeval timer_start, timer_end;
gettimeofday(&timer_start, NULL);

/* Put this right after the code you want to time: */
gettimeofday(&timer_end, NULL);
double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0;
printf("Time spent: %.6f\n", timer_spent);

batch set day / date / time variables

// set day / day / time variables in batch
cd %temp%
echo.|date>datevar.bat
echo.|time>timevar.bat
echo set DATEvar=%%4>current.bat
call datevar
echo set DAYvar=%%3>current.bat
call datevar
echo set TIMEvar=%%3>current.bat
call timevar
echo %DAYvar%
echo %DATEvar%
echo %TIMEvar%

How to get a String with just the date from a Calendar

// Given Calendar cal

String date = cal.get(Calendar.DAY_OF_MONTH) + "-" 
    + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.YEAR);

How to add days to a date

// You should use the class Calendar.

// Given Calendar cal and int n with the number of days
cal.add(Calendar.DATE, n);

How to convert a String with a date to a Calendar

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);

Ruby: set an interval for your Time (useful for ranges, especially)

class Time
  def set_interval(seconds)
    @interval = seconds
  end
  def succ
    @interval ||= 1
    ret = self + @interval
    ret.set_interval(@interval)
    ret
  end
end

Unix time

Function UnixTime(gmtHrsOffset)
	UnixTime = DateDiff("s", "1/1/1970 00:00:00", Now()) - (3600 * gmtHrsOffset)
End Function

Repsonse.Write(UnixTime(-5)) 'E.S.T.


Adding in the GMT offset allowed this to match PHP's time() function on a separate server.

StopWatch - Why is my app so slow ???

StopWatch is a small class allowing to manage multiple concurrent stopwatches to measure time elapsed between two calls. Nothing is done while time is rolling (no thread, no loop), it only stores and compares timestamps.

import java.util.Hashtable;

public class StopWatch {
    private static final Hashtable startTime = new Hashtable();
    
    public static void start(String id){
        startTime.put(id,new Long(System.currentTimeMillis()));
    }

    public static long stop(String id){
        return System.currentTimeMillis() - ((Long)startTime.remove(id)).longValue();
    }
}


Example of use :
public main(String[] args) {
    // Start a global stopwatch
    StopWatch.start("GLOBAL");

    // evaluate time used by task 1
    StopWatch.start("TASK1");
    executeTask1();
    System.out.println("Time elapsed for task 1 : " + StopWatch.stop("TASK1") + "ms";

    // evaluate time used by task 2
    StopWatch.start("TASK2");
    executeTask2();
    System.out.println("Time elapsed for task 2 : " + StopWatch.stop("TASK2") + "ms";

    // Display time elapsed for full processing
    System.out.println("Total processing time : " + StopWatch.stop("GLOBAL") + "ms";
}

terminal *live* text graph in php, unix terminal

I often run php scripts from the command line in linux and need a way to view time series of data, often I need to see the number of mysql queries or want to view other stats.
That look like the following, it moves from right to left in real time as the data points change.
+local mysql: 565 queries/sec 189----------------------+
 |                                                      |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                                       %              |
 |                   %                   %              |
 | %   %%   %% % %  %%%%%                % %%        %% |
 |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% %%%%%%  %%%%%|
 |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|
+-------------------------------------high: 100, low: 1+

Just copy and past the following code into a file called "termgraph.php" and change the mysql password (if needed) and change the database name (if needed as it it set to work with the default mysql install), then run it and see, very easy to modify this to work with whatever you need. In addition, you can also do colored graphs in terminals using another function I am going to post here.

This needs a couple changes to work with your mysql database, but the graphs are fun to watch, hope you enjoy this.

<?
// see *EDIT* below for one other change you will need to make
function DatabaseConnect() {
    if (!($mylink = mysql_connect("localhost", "root", ""))){
            print  "ERROR";
            exit;
        }//fi
        mysql_select_db("test") or die(mysql_error());
}// end function
DatabaseConnect();

/*
    remove flatspots in array..
    areas where the data does not change.
    this is good for data that only changes during certain times
    and the dead time has no bearing on the changes.
    -joeldg
*/
function remove_flatspots($arr, $pkey=true){
    while(list($key,$val) = each($arr)){
        if($val <> $oldval){
            if($pkey == true){
                $ret[$key] = $val;
            }else{
                $ret[] = $val;
            }
        }
        $oldval = $val;
    }
    return $ret;
}
/*
    take two arrays, remove the flatspots in the first.
    return an array containing the first with it's corresponding
    values in the second array..
    -joeldg
*/
function dual_remove_flatspots($arr1, $arr2, $pkey=true){
    while(list($key,$val) = each($arr1)){
        if($val <> $oldval){
            if($pkey == true){
                $ret[0][$key] = $val;
                $ret[1][$key] = $arr2[$key];
            }else{
                $ret[0][] = $val;
                $ret[1][] = $arr2[$key];
            }
        }
        $oldval = $val;
    }
    return $ret;
}

// return lowest val of array -joeldg
function least($inarr){
    $ret = $inarr[0];
    for($i=0;$i<count($inarr);$i++){
        if(intval($inarr[$i]) <= $ret){ $ret = $inarr[$i]; }
    }//rof
    return $ret;
}// end function
// return higest val of array -joeldg
function most($inarr){
    while(list($key,$val) = each($inarr)){
        if ($ret==""){$ret = $val;}
        if($ret <= intval($val)){ $ret = $val; }
    }//rof
    return $ret;
}// end function
/*
	array normalize function 
	-joeldg
*/
function normalize($arr,$LO=0.01,$HI=0.99)
{
  $Min = +2147483647;
  $Max = -2147483647;
  for ($a=0; $a<count($arr); $a++) {
    $Min = min($Min, $arr[$a]);
    $Max = max($Max, $arr[$a]);
  }
  $Mean = 0;
  for ($a=0; $a<count($arr); $a++) {
    $div = $Max-$Min;
    if($div == 0){$div = 1;}
    $arr [$a] = (($arr[$a]-$Min) / ($div)) * ($HI-$LO) + $LO;
    $Mean += $arr[$a] / count($arr);
  }
  return $arr;
}
/*
	array normalize function, preserve key
	-joeldg
*/
function normalizekey($arr,$LO=0.01,$HI=0.99)
{
  $Min = +2147483647;
  $Max = -2147483647;
  while(list($key, $a)=each($arr)){
    $Min = min($Min, $arr[$key]);
    $Max = max($Max, $arr[$key]);
  }
  reset($arr);
  $Mean = 0;
  while(list($key,$a)=each($arr)){
    $div = $Max-$Min;
    if($div == 0){$div = 1;}
    $arr [$key] = (($arr[$key]-$Min) / ($div)) * ($HI-$LO) + $LO;
    $Mean += $arr[$key] / count($arr);
  }
  $retarr[0] = $arr;
  $retarr[1][0]=$Max;
  $retarr[1][1]=$Min;
  
  return $retarr;
}

/*
display a textual graph in an xterm.
written because I want to view timeseries data and not have to jump over to a browser.
*/
#
#    transform an array point to a position within the total
#
function point2arr($point, $total=20){
    $p = round($point);
    for($a=0;$a<$total;$a++){
        if($a <= $p){
            $ret[] = "%";#chr(127);
        }else{
            $ret[] = " ";
        }
    }
    return $ret;
}
// get the width and height of a unix terminal
function get_term_specs(){
    $b = `stty -a`;
    $c = explode("\n",$b);
    $d = explode(";", $c[0]);
    $f = explode(" ",$d[1]);
    $ret[h] = $f[2];
    $f = explode(" ",$d[2]);
    $ret[w] = intval($f[2]);    
    return $ret;
}

function genchars($char, $total, $title="", $echo=false, $way=STR_PAD_RIGHT){
    $ret .= "+";
    #if($echo){ echo termcolored("+", WHITE); }
    #if(!$echo){ $ret .= "+"; }
        #$back = termcolored($title, "YELLOW");
        $back = $title;
        $ret .= str_pad($back, $total+strlen($back)-strlen($title)-2, $char, $way);
    #if(!$echo){ $ret .= "+"; }
    #if($echo){ echo termcolored("+", WHITE); }
    $ret .= "+";
    return $ret;
}
// vertical graph
function print_vert_graph($arr, $total=20, $border="-"){
    $arr = normalize($arr, 0, $total);
    $out[] = genchars($border, $total);
    for($a=0;$a<count($arr);$a++){
        $out[] = point2arr($arr[$a], $total);
    }
    $out[] = genchars($border, $total);
    for($a=0;$a<count($out);$a++){
        for($b=0;$b<count($out[$a]);$b++){
            echo $out[$a][$b];
        }
        echo "\n";
    }
    
}
// transform matrix 
function transformmat($arr,$total=20){
    $width = count($arr[0]);
    $c=$width-1;
    for($w=0;$w<$width;$w++){
        for($a=0;$a<count($arr);$a++){
            $ret[$c][$a] = $arr[$a][$w];
        }
        $c--;
    }
    return $ret;
}
// horizontal graphing
function print_horz_graph($arr, $total=20, $border="-", $title="", $w=""){
    if($w <> ""){
        array_reverse($arr);
        $end = count($arr)-1;
        reset($arr);
        while(list($key,$val)=each($arr)){
            $newarr[] = $arr[$key];
            $end--;
            if($end <= 0){break;}
        }
        #array_reverse($newarr);
        $arr=$newarr;
    }
    #print_R($arr);
    $bottom = "high: ".most($arr).", low: ".least($arr);
    $arr = normalize($arr, 0, $total-2);
    #$out[] = genchars($border, $total);
    for($a=0;$a<count($arr);$a++){
        $out[] = point2arr($arr[$a], $total);
    }
    $out = transformmat($out,$total);
    $ret .= genchars("-", count($out[0])+2, $title, false);
    #echo "\n";
    $ret .= "\n";
    for($a=0;$a<count($out);$a++){
        #echo termcolored("|", WHITE);
        $ret .=  " |";
        for($b=0;$b<count($out[$a]);$b++){
            $ret .= $out[$a][$b];
        }
        $ret .= "|";
        #echo termcolored("|", WHITE);
        $ret .= "\n";
    }
    $ret .= genchars("-", count($out[0])+2, $bottom, false, STR_PAD_LEFT);
    $ret .= "\n";
    return $ret;
    
}

// *EDIT* change mysql to whatever table you want to test
// this default should work for fun..
$db_list = mysql_list_tables("mysql");
while ($row = mysql_fetch_row($db_list)) {
   $tables[] = trim("{$row[0]}");
}
$testarr_z = array();
$testarr = array();
$qcount = 0;
$start = microtime(true);

while(1){
	$hw = get_term_specs();
	array_pad($testarr_z, ($hw['w'] - 6), 0);

	array_pad($testarr, ($hw['w'] - 6), 0);
	$time_start = microtime(true);
	$tab = $tables[rand(0,count($tables))];
	
	$amt = rand(500, 5000);
	$sql = "SELECT * FROM $tab LIMIT 0,$amt";
	$res = mysql_db_query("oracle", $sql);
	$qcount++;
	
	$time_end = microtime(true);
	$time = $time_end - $time_start;
	$testarr_z[] = $time;
	@mysql_free_result($res);
	
	if(count($testarr_z)> ($hw['w'] - 6)){
		array_shift($testarr_z);
	}
	$testarr = normalize($testarr_z, 1, 100);

	$testarr = remove_flatspots($testarr, false);
	$tottime = $time_end - $start;
	$qps = round($qcount/$tottime);
	echo print_horz_graph($testarr, $hw['h']-3, "-", "local mysql: $qcount queries/sec $qps", $hw['w']-1);

}

?>

time since

// returns the time since $original (unix date)

function time_since($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );
    
    $today = time(); /* Current unix time  */
    $since = $today - $original;
	
	if($since > 604800) {
		$print = date("M jS", $original);
	
		if($since > 31536000) {
				$print .= ", " . date("Y", $original);
			}

		return $print;

	}
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            // DEBUG print "<!-- It's $name -->\n";
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    return $print . " ago";

}
« Newer Snippets
Older Snippets »
Showing 11-20 of 47 total