Easy runtime meter


SDK Version: 
M3

tIn this tutorial I'll show you a simple time meter example. It can be used for checking the running time of your algorithms. It's useful when you need to decide wich solution is faster/better than an other. It just a simple helper code, but if you made some changes on it, can be a part of any final project, like benchmarking or testing workarounds.

First of all, create a new android project in Eclipse. Then make the basic layout (main.xml) looks like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >
  6.  
  7.     <Button
  8.         android:layout_gravity="center_horizontal"
  9.         android:id="@+id/StartButton"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="@string/startbutton";
  13.         android:layout_marginBottom="20dp"
  14.         android:layout_margin="5dp"
  15.         android:textSize="20sp"
  16.         android:padding="20dp" />
  17.    
  18.     <TextView
  19.         android:layout_width="fill_parent"
  20.         android:layout_height="fill_parent"
  21.         android:text=""
  22.         android:textSize="20sp"
  23.         android:gravity="center_horizontal"
  24.         android:id="@+id/TimeTextView" />
  25.    
  26.  
  27. </LinearLayout>

The strings.xml looks like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4.     <string name="hello">Hello World, TimerTestActivity!</string>
  5.     <string name="app_name">TimerTest</string>
  6.     <string name="startbutton">Start</string>
  7.     <string name="TimerText">Time meter</string>
  8.     <string name="TimerPlaseWaitText">Please wait…</string>
  9.     <string name="TestRunningTime">Test running time: </string>
  10.  
  11. </resources>

Now the java coding:
The application using a simple thread and a handler to show the running time.
When pressing the Start button the thread starts and and do the stuff which is in the run block. In the first line of onclick, the current time will be saved. After that the handler get an empty message which means the running is finished.
The handler object calculates the time and show it on the ui.

  1. @Override
  2.  public void onCreate(Bundle savedInstanceState) {
  3.   super.onCreate(savedInstanceState);
  4.   setContentView(R.layout.main);
  5.  
  6.   mTimeText = (TextView) findViewById(R.id.TimeTextView);
  7.  
  8.   mStartButton = (Button) findViewById(R.id.StartButton);
  9.  
  10.   mStartButton.setOnClickListener(new OnClickListener() {
  11.  
  12.    @Override
  13.    public void onClick(View v) {
  14.     mStartTime = System.currentTimeMillis();
  15.  
  16.     final ProgressDialog progDialog;
  17.     progDialog = ProgressDialog.show(TimerTestActivity.this,
  18.       getText(R.string.TimerText),
  19.       getText(R.string.TimerPlaseWaitText), true);
  20.     new Thread() {
  21.      public void run() {
  22.       try {
  23.        // Place your algorithm here
  24.        //
  25.        
  26.        for (int i = 1; i < 1500; i++) {
  27.         for (int j = 1; j < 1500; j++) {
  28.          Math.log(Math.pow(i, j));
  29.         }
  30.        }
  31.  
  32.        //
  33.        //
  34.        mHandler.sendEmptyMessage(0);
  35.       } catch (Exception e) {
  36.        Log.d("TimerTestException:";, e.getMessage());
  37.       }
  38.       progDialog.dismiss();
  39.      }
  40.     }.start();
  41.    }
  42.   });
  43.  }
  44.  
  45.  private Handler mHandler = new Handler() {
  46.   public void handleMessage(Message msg) {
  47.    mDifference = System.currentTimeMillis() - mStartTime;
  48.    double formatedTime = ((double) mDifference) / 1000;
  49.    mTimeText.setText(getText(R.string.TestRunningTime) + " "
  50.      + formatedTime + " sec");
  51.    mTimeText.setVisibility(TextView.VISIBLE);
  52.   }
  53.  };

t1

t1

t1

You can download the full project from there: TimerTest.zip