KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > performance > SystemTime


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.performance;
18
19 /**
20    Measures the time required to make a System.currentTimeMillis() and
21    Thread.currentThread().getName() calls.
22
23    <p>On an 233Mhz NT machine (JDK 1.1.7B) the
24    System.currentTimeMillis() call takes under half a microsecond to
25    complete whereas the Thread.currentThread().getName() call takes
26    about 4 micro-seconds.
27
28 */

29 public class SystemTime {
30
31   static int RUN_LENGTH = 1000000;
32
33   static
34   public
35   void main(String JavaDoc[] args) {
36     double t = systemCurrentTimeLoop();
37     System.out.println("Average System.currentTimeMillis() call took " + t);
38
39     t = currentThreadNameloop();
40     System.out.println("Average Thread.currentThread().getName() call took "
41                + t);
42     
43   }
44
45   static
46   double systemCurrentTimeLoop() {
47     long before = System.currentTimeMillis();
48     long l;
49     for(int i = 0; i < RUN_LENGTH; i++) {
50       l = System.currentTimeMillis();
51     }
52     return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
53   }
54
55   static
56   double currentThreadNameloop() {
57     long before = System.currentTimeMillis();
58     String JavaDoc t;
59     for(int i = 0; i < RUN_LENGTH; i++) {
60       t = Thread.currentThread().getName();
61     }
62     return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
63   }
64 }
65
Popular Tags