/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

  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. package org.apache.log4j.performance;
  18. /**
  19. Measures the time required to make a System.currentTimeMillis() and
  20. Thread.currentThread().getName() calls.
  21. <p>On an 233Mhz NT machine (JDK 1.1.7B) the
  22. System.currentTimeMillis() call takes under half a microsecond to
  23. complete whereas the Thread.currentThread().getName() call takes
  24. about 4 micro-seconds.
  25. */
  26. public class SystemTime {
  27. static int RUN_LENGTH = 1000000;
  28. static
  29. public
  30. void main(String[] args) {
  31. double t = systemCurrentTimeLoop();
  32. System.out.println("Average System.currentTimeMillis() call took " + t);
  33. t = currentThreadNameloop();
  34. System.out.println("Average Thread.currentThread().getName() call took "
  35. + t);
  36. }
  37. static
  38. double systemCurrentTimeLoop() {
  39. long before = System.currentTimeMillis();
  40. for(int i = 0; i < RUN_LENGTH; i++) {
  41. System.currentTimeMillis();
  42. }
  43. return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
  44. }
  45. static
  46. double currentThreadNameloop() {
  47. long before = System.currentTimeMillis();
  48. for(int i = 0; i < RUN_LENGTH; i++) {
  49. Thread.currentThread().getName();
  50. }
  51. return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;
  52. }
  53. }