简体   繁体   English

程序执行时间

[英]Program execution time

Is there a software/website where I can submit my C, C++ and Java codes and get statistics like the program execution time, memory used ? 是否有软件/网站,我可以提交我的C,C ++和Java代码,并获得程序执行时间,使用的内存等统计信息? I'm interested in doing a comparision of the same code in different languages and getting an estimate of which data structures/operations are better suited for which language. 我有兴趣对不同语言的相同代码进行比较,并估计哪种数据结构/操作更适合哪种语言。

Do it yourself. 自己做。 On a *nix machine (eg Linux and OSX) just run from the terminal: 在* nix机器(例如Linux和OSX)上,只需从终端运行:

time java YourJavaProgram

or 要么

time ./YourCProgram

On Windows, you can write a little batch script to do the equivalent. 在Windows上,您可以编写一个小批处理脚本来执行等效操作。

您可以将程序粘贴到ideone ,它将告诉您执行时间和内存消耗。

The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime(); 程序的总使用/可用内存可以通过java.lang.Runtime.getRuntime()在程序中获得;

The runtime has several method which relates to the memory. 运行时有几种与内存相关的方法。 The following coding example demonstrate its usage. 以下编码示例演示了其用法。

package test;

import java.util.ArrayList;
import java.util.List;

public class PerformanceTest {
  private static final long MEGABYTE = 1024L * 1024L;

  public static long bytesToMegabytes(long bytes) {
    return bytes / MEGABYTE;
  }

  public static void main(String[] args) {
    // I assume you will know how to create a object Person yourself...
    List<Person> list = new ArrayList<Person>();
    for (int i = 0; i <= 100000; i++) {
      list.add(new Person("Jim", "Knopf"));
    }
    // Get the Java runtime
    Runtime runtime = Runtime.getRuntime();
    // Run the garbage collector
    runtime.gc();
    // Calculate the used memory
    long memory = runtime.totalMemory() - runtime.freeMemory();
    System.out.println("Used memory is bytes: " + memory);
    System.out.println("Used memory is megabytes: "
        + bytesToMegabytes(memory));
  }
} 

Use System.currentTimeMillis() to get the start time and the end time and calculate the difference. 使用System.currentTimeMillis()获取开始时间和结束时间并计算差异。

package performance.test;

class TimeTest1 {
  public static void main(String[] args) {

    long startTime = System.currentTimeMillis();

    long total = 0;
    for (int i = 0; i < 10000000; i++) {
      total += i;
    }

    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println(elapsedTime);
  }
} 

That's called benchmarking and you can do it yourself. 这称为基准测试 ,你可以自己做。 The thing is that many other variables affect the results, so it would be best to control the environment and measure the performance of your program trying to eliminate as much external variables as possible. 问题是许多其他变量会影响结果,因此最好控制环境并测量程序的性能,尽量消除尽可能多的外部变量。

For example, it is advisable to do all the measurements of all the programs on the same machine, with the same environment, as much as that can be achievable. 例如,建议对同一台机器上的所有程序进行所有测量,并使用相同的环境,尽可能多地进行测量。

if you want to get benchmark of functions, using "time" is not fair. 如果你想获得功能的基准,使用“时间”是不公平的。 You should write following in your language. 你应该用你的语言写下面的内容。

time_before = get_now()
// do something
span = get_time() - time_before

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

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