Logarithmic way of printing program status

July 3rd, 2005


Imagine, you are working on a program that will manipulate an unknown length file. Printing the output to tell user is necessary for telling the user that the program is not halting. Waiting with a blank terminal with only a blinking cursor is no fun. So, we need to print something to tell the user. The question is what to print. The most basic idea would be doing something like

for(i=0;i<max;i++){
    if(i%200 == 0){
        System.out.println(i);
    }
}

This solution will print every 200 number. It looks like a good solution for status printing but there is a problem with that 200. How do we choose that number 200? In the code given above, suppose max = 5×10^6, the code above will print 25000 status notification. That’s way too many.This question is easy if we know the max number beforehand. For example, if we want about 20 status notification to be printed, we can do the math and find the interval number.

Unfortunately, in the real world. Most of the time we don’t know the length of the stream(file) before hand. So, we need to come up with a solution that won’t print too many on a large stream. Here is one solution

10
20
.
.
.
90
100
200
300
400

This looks like a logarithmic response which won’t print too many notification you will get the amout of notification about right. So, I won’t talk more about this, I’ll just let you play with it. :) The code can be found below

/*
*   Copyrighted by Piti Ongmongkolkul 7/3/2005
*/
import java.util.*;
class ExponentialPrinting{
    //this class will be used to print out data in the following manner
    //10 20...90 100 200....900 1000 2000.....
    //run main for example output
    //see main for example usage
    //useful for reading and printing process of unknownlength stream
    int exponent;
    int lastPrintTest;
    int divisor;

    public ExponentialPrinting(int exponentArg, int startPrintingArg){
        exponent = exponentArg;
        lastPrintTest = startPrintingArg;
        divisor = Math.round((float)Math.floor(Math.pow(exponent,lastPrintTest)));
    }
    
    public ExponentialPrinting(){
        this(10,2);
    }
    
    //reset the counter
    public void reset(){
        divisor = Math.round((float)Math.floor(Math.pow(exponent,lastPrintTest)));
    }
    
    public boolean shouldPrint(int i){
        double test = Math.log((double)i) / Math.log((double)exponent);
        if(test - lastPrintTest > 1 ){
            divisor = divisor*exponent;
            lastPrintTest = Math.round((float)Math.floor(test));
        }
        return i%divisor == 0;
    }
    
    public static void main(String[] args){
        ExponentialPrinting ep = new ExponentialPrinting();
        for(int i =0 ; i<3400000; ++i){
            if(ep.shouldPrint(i)) System.out.println(i);
        }
    }
}

Entry Filed under: Physics, current work, Programming

1 Comment Add your own

  • 1. Tee’s Blog » &hellip  |  July 4th, 2005 at 5:37 pm

    […] It’s quite a chore to make a histogram. It comes to me many time that I have to make a histogram for a list of numbers. I had to rewrite the program everytime. But, this time I’ll just write a good one and publish it so that other people(including me) won’t have to do this chore again. Without saying much here you go Histogram.tar.gz This program use Logarithmic way of printing program status. […]

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

July 2005
M T W T F S S
    Aug »
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts