Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package calculatempo;
  6.  
  7. import java.lang.management.ThreadMXBean;
  8. import java.lang.management.ManagementFactory;
  9.  
  10. /**
  11. *
  12. * @author Gilson
  13. */
  14. public class BenchMark {
  15.  
  16. private long inicio = 0;
  17. private long fim = 0;
  18. //private long tempo = fim - inicio;
  19.  
  20. public BenchMark() {
  21. inicio = 0;
  22. fim = 0;
  23. }
  24.  
  25. public double getTempo() {
  26. return (fim - inicio);
  27. }
  28.  
  29. public void reset() {
  30. inicio = 0;
  31. fim = 0;
  32. }
  33.  
  34. public void startWatch() {
  35. inicio = System.nanoTime();
  36. }
  37.  
  38. public void endWatch() {
  39. fim = System.nanoTime();
  40. }
  41.  
  42. public void startCpu() {
  43. inicio = getCpuTime();
  44. }
  45.  
  46. public void endCpu() {
  47. fim = getCpuTime();
  48. }
  49.  
  50. /** Get CPU time in nanoseconds. */
  51. public long getCpuTime() {
  52. ThreadMXBean bean = ManagementFactory.getThreadMXBean();
  53. return bean.isCurrentThreadCpuTimeSupported()
  54. ? bean.getCurrentThreadCpuTime() : 0L;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement