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 1-2 of 2 total  RSS 

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";
}

Optimize all your mysql databases automatically

// description of your code here
Use this script preferably with a cronjob to automatically optimize all the tables in your mysql databases. Under root privilleges the script will search for all the databases in your server and will optimize them all. Hope you will like it as it took me more than 3 hours to write it

<?php
echo '<pre>' . "\n\n";
set_time_limit( 100 );

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;

//Connection variables :
$h = 'localhost';
$u = 'root';
$p = 'password';

$dummy_db = 'mysql';//The php->mysql API needs to connect to a database even when executing scripts like this. If you got an error from this(permissions), just replace this with the name of your database

$db_link = mysql_connect($h,$u,$p);

$res = mysql_db_query($dummy_db, 'SHOW DATABASES', $db_link) or die('Could not connect: ' . mysql_error());
echo 'Found '. mysql_num_rows( $res ) . ' databases' . "\n";
$dbs = array();
while ( $rec = mysql_fetch_array($res) )
{
$dbs [] = $rec [0];
}

foreach ( $dbs as $db_name )
{
echo "Database : $db_name \n\n";
$res = mysql_db_query($dummy_db, "SHOW TABLE STATUS FROM `" . $db_name . "`", $db_link) or die('Query : ' . mysql_error());
$to_optimize = array();
while ( $rec = mysql_fetch_array($res) )
{
if ( $rec['Data_free'] > 0 )
{
$to_optimize [] = $rec['Name'];
echo $rec['Name'] . ' needs optimization' . "\n";
}
}
if ( count ( $to_optimize ) > 0 )
{
foreach ( $to_optimize as $tbl )
{
mysql_db_query($db_name, "OPTIMIZE TABLE `" . $tbl ."`", $db_link );
}
}
}

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 6);
echo 'Parsed in ' . $total_time . ' secs' . "\n\n";
?>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS