简体   繁体   English

如何让我的程序(java)有效地使用 CPU 时间?

[英]How do I get the CPU time used effectively by my program (java)?

I'm working in a code in java which I must send to an external computer to run.我正在使用 java 代码,我必须将其发送到外部计算机才能运行。 So far in my computer I was using this function to get an idea of time (real time was enough until now)到目前为止,在我的计算机中,我正在使用此功能来了解时间(直到现在,实时就足够了)

     long start;
     long end;
    start = System.currentTimeMillis();
    //
    %code
    //
    end   = System.currentTimeMillis();
    time=(float) ((end - start)/1000.);

But then I realized, the computer distribute the CPU time expended among the users, so the execution is "paused" several seconds until is the turn of my run again, therefore I get a time which is not accurate.但后来我意识到,计算机将 CPU 时间分配给用户,因此执行会“暂停”几秒钟,直到轮到我再次运行,因此我得到的时间不准确。 I investigated and implemented this example based on the answer of Recording Program's CPU Time :我根据Recording Program's CPU Time的回答调查并实现了这个例子:

    try {

       ThreadMXBean thread = ManagementFactory.getThreadMXBean();

       long cpu = thread.getCurrentThreadCpuTime();
       long user= thread.getCurrentThreadUserTime() ;
       Thread.sleep(300);
       int i=0;
       while(i<100000){
           System.out.println("hola");
           i++;
       }
       cpu =( thread.getCurrentThreadCpuTime() - cpu ) ;
       user=( thread.getCurrentThreadUserTime() -user );
       System.out.println("Cpu:"+cpu);
       System.out.println("User:"+user);
   }
   catch (InterruptedException _) {}

for example I've gotten these results:例如我得到了这些结果:

Cpu:1218750000 > User:593750000处理器:1218750000 > 用户:593750000

Cpu:1453125000 >User:750000000处理器:1453125000 > 用户:750000000

is thread.getCurrentThreadUserTime() the function I need for my case? thread.getCurrentThreadUserTime() 是我的案例所需的函数吗?

I have never used this ThreadMXBean although it would have been useful sometimes in the past :)我从未使用过这个 ThreadMXBean,尽管它在过去有时会很有用:)

Anyway, the JavaDoc tells无论如何,JavaDoc 告诉

long getCurrentThreadUserTime()长 getCurrentThreadUserTime()

Returns the CPU time that the current thread has executed in user mode in nanoseconds.返回当前线程在用户模式下执行的 CPU 时间(以纳秒为单位)。 The returned value is of nanoseconds precision but not necessarily nanoseconds accuracy.返回值是纳秒精度,但不一定是纳秒精度。 This is a convenient method for local management use and is equivalent to calling:这是本地管理使用的便捷方法,相当于调用:

getThreadUserTime(Thread.currentThread().getId()); getThreadUserTime(Thread.currentThread().getId());

Returns: the user-level CPU time for the current thread if CPU time measurement is enabled;返回: 如果启用 CPU 时间测量,则当前线程的用户级 CPU 时间; -1 otherwise. -1 否则。 Throws: UnsupportedOperationException - if the Java virtual machine does not support CPU time measurement for the current thread.抛出: UnsupportedOperationException - 如果 Java 虚拟机不支持当前线程的 CPU 时间测量。 See Also: getCurrentThreadCpuTime(), isCurrentThreadCpuTimeSupported(), isThreadCpuTimeEnabled(), setThreadCpuTimeEnabled(boolean)另请参见:getCurrentThreadCpuTime()、isCurrentThreadCpuTimeSupported()、isThreadCpuTimeEnabled()、setThreadCpuTimeEnabled(boolean)

So what you are doing seems to be correct.所以你在做什么似乎是正确的。 You may ask yourself why the numbers are so high, they are returned in nanoseconds .您可能会问自己为什么这些数字如此之高,它们以nanoseconds为单位返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM