Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Making A Countup Timer In Java
New Posts  All Forums:Forum Nav:

Making A Countup Timer In Java

post #1 of 4
Thread Starter 
So I am pretty new to Java, but I had a simple idea for a (ineffective) benchmark type program. Ineffective because it only uses about 20% of the CPU, but you get the point biggrin.gif

Anyway, I made this counting program that counts to 100,000,000, then stops. What I am trying to do is implement a timer that will count how long it takes for the PC to achieve this. Preferably with milliseconds, but since I am still a beginner, and sort of timer would be fine, then I can figure out other timers later.

Any help would be great!

Here is my code now:
Code:
public class Main
{
    public static void main(String[] args){

        int i = 0;

        while (i < 10000000){
            i++;
            System.out.println(i);
        }
    }
}
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #2 of 4
Code:
import java.util.Timer;

/**
* An example on multiple timers.
*
* @author http://www.gammelsaeter.com/
*/
public class TimerExample {

public static void main(String[] args) {
Timer timer1 = new Timer();             // Get timer 1
Timer timer2 = new Timer();             // get timer 2

long delay1 = 5*1000;                   // 5 seconds delay
long delay2 = 3*1000;                   // 3 seconds delay

// Schedule the two timers to run with different delays.
timer1.schedule(new Task("object1"), 0, delay1);
timer2.schedule(new Task("Object2"), 0, delay2);
}

}


http://www.gammelsaeter.com/programming/simple-timer-example-in-java/
Computah
(18 items)
 
  
CPUMotherboardGraphicsRAM
2500k @4.5 Asus P8Z68-V GEN3 MSI 770GTX Twin Frozr Gaming 2x4Gb Ripjaws X 
Hard DriveHard DriveCoolingOS
2x HyperX Fury 240gb Raid0 WD Red 3TB NH-D14 Windows 10 Pro 64b 
MonitorMonitorKeyboardPower
Acer X233H 23" BenQ G2420HDB 24" Ducky Shine 3 TKL Corsair TX 750 
CaseMouseMouse PadAudio
HAF 912 PLUS Logitech G400s Roccat Taito Beyerdynamic DT-770 Pro 80ohm 
  hide details  
Reply
Computah
(18 items)
 
  
CPUMotherboardGraphicsRAM
2500k @4.5 Asus P8Z68-V GEN3 MSI 770GTX Twin Frozr Gaming 2x4Gb Ripjaws X 
Hard DriveHard DriveCoolingOS
2x HyperX Fury 240gb Raid0 WD Red 3TB NH-D14 Windows 10 Pro 64b 
MonitorMonitorKeyboardPower
Acer X233H 23" BenQ G2420HDB 24" Ducky Shine 3 TKL Corsair TX 750 
CaseMouseMouse PadAudio
HAF 912 PLUS Logitech G400s Roccat Taito Beyerdynamic DT-770 Pro 80ohm 
  hide details  
Reply
post #3 of 4
Thread Starter 
Ahh, thanks! Will try that out!
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #4 of 4
Code:
public class Main
{
    public static void main(String[] args){

        long startTime = System.currentTimeMillis();
        int i = 0;

        while (i < 10000000){
            i++;
            System.out.println(i);
        }
      long stopTime = System.currentTimeMillis();
      long elapsedTime = stopTime - startTime;
      System.out.println(elapsedTime);
    }
}

or use the stopwatch class
Code:
public class Main
{
    public static void main(String[] args){

       Stopwatch timer = new Stopwatch().start();

        int i = 0;

        while (i < 10000000){
            i++;
            System.out.println(i);
        }
      timer.stop();
      System.out.println(timer.getElapsedTime());
    }
}

Edited by surfbumb - 12/10/12 at 4:24pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Making A Countup Timer In Java