PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/performance/java/org/apache/log4j/performance/SystemTime.java

#
Java | 63 lines | 29 code | 8 blank | 26 comment | 2 complexity | e63b927deb97368f6429e9d55096bca9 MD5 | raw file
Possible License(s): Apache-2.0
 1/*
 2 * Licensed to the Apache Software Foundation (ASF) under one or more
 3 * contributor license agreements.  See the NOTICE file distributed with
 4 * this work for additional information regarding copyright ownership.
 5 * The ASF licenses this file to You under the Apache License, Version 2.0
 6 * (the "License"); you may not use this file except in compliance with
 7 * the License.  You may obtain a copy of the License at
 8 * 
 9 *      http://www.apache.org/licenses/LICENSE-2.0
10 * 
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.apache.log4j.performance;
19
20/**
21   Measures the time required to make a System.currentTimeMillis() and
22   Thread.currentThread().getName() calls.
23
24   <p>On an 233Mhz NT machine (JDK 1.1.7B) the
25   System.currentTimeMillis() call takes under half a microsecond to
26   complete whereas the Thread.currentThread().getName() call takes
27   about 4 micro-seconds.
28
29*/
30public class SystemTime {
31
32  static int RUN_LENGTH = 1000000;
33
34  static
35  public 
36  void main(String[] args) {    
37    double t = systemCurrentTimeLoop();
38    System.out.println("Average System.currentTimeMillis() call took " + t);
39
40    t = currentThreadNameloop();
41    System.out.println("Average Thread.currentThread().getName() call took " 
42		       + t);
43    
44  }
45
46  static
47  double systemCurrentTimeLoop() {
48    long before = System.currentTimeMillis();
49    for(int i = 0; i < RUN_LENGTH; i++) {
50      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    for(int i = 0; i < RUN_LENGTH; i++) {
59      Thread.currentThread().getName();
60    }
61    return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;    
62  }  
63}