程序执行时间

发布于 2024-11-16 08:56:14 字数 99 浏览 1 评论 0原文

有没有一个软件/网站可以提交我的 C、C++ 和 Java 代码并获取程序执行时间、使用的内存等统计信息?我有兴趣对不同语言的相同代码进行比较,并估计哪些数据结构/操作更适合哪种语言。

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 ? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戏舞 2024-11-23 08:56:14

自己做吧。在 *nix 机器(例如 Linux 和 OSX)上只需从终端运行:

time java YourJavaProgram

或者

time ./YourCProgram

在 Windows 上,您可以 编写一个小批处理脚本来执行相同的操作。

Do it yourself. On a *nix machine (e.g. Linux and OSX) just run from the terminal:

time java YourJavaProgram

or

time ./YourCProgram

On Windows, you can write a little batch script to do the equivalent.

面如桃花 2024-11-23 08:56:14

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

You can paste your program at ideone and it will tell you the execution time and memory consumption.

请爱~陌生人 2024-11-23 08:56:14

这就是所谓的基准测试,您可以自己做。问题是许多其他变量会影响结果,因此最好控制环境并测量程序的性能,尝试消除尽可能多的外部变量。

例如,建议尽可能在同一机器上、相同环境下对所有程序进行所有测量。

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.

蓝海似她心 2024-11-23 08:56:14

如果你想获得功能的基准,使用“时间”是不公平的。您应该用您的语言写下以下内容。

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

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
十级心震 2024-11-23 08:56:14

程序中已使用/空闲内存总量可以在程序中通过java.lang.Runtime.getRuntime()获取;

运行时有几个与内存相关的方法。以下编码示例演示了其用法。

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));
  }
} 

使用 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);
  }
} 

The total used / free memory of an program can be obtained in the program via 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.

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);
  }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文