//C++ code
#include <iostream>
#include <ctime>
#include <stdio.h>
using namespace std;
int main() {
int tStart=clock();
int a[10000000][2];
for(int i=0;i<10000000;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=0;
}
}
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
return 0;
}
// Java Code
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
long startTime = System.currentTimeMillis();
int n=10000000;
int [][]table=new int[n][2];
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime/1000.0);
}
}
- C ++运行时:0.00s(我故意将循环初始化数组以防万一在java中数组自动被初始化为0。
- Java Runtime:3.696s
- 我正在为一个有竞争力的网站编写一个代码,该代码需要一个大小为M [10 ^ 7] [24]的备忘录表,我不知道因为我对java很新,它会耗尽内存,之后在大多数测试用例中获得TLE我意识到在java中花费了大量时间创建表,所以,我将jth值从24减少到2仅用于测试目的,并发现只需创建数组需要3.5秒而在c ++中,时间是0.00秒。那么,这可能是造成这种巨大差异的原因。
- 由于我需要提交代码并且代码与运行时O(nlog(n))+ O(n ^ 2)和空间O(log(n))相当复杂,有没有办法创建数组尺寸M [10 ^ 7] [24]在java?
中