Last active
May 19, 2016 13:39
-
-
Save mohemohe/105d2a3861e9d6af20c9704248798b7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.ghippos.arraytest; | |
| import java.util.*; | |
| public class Main { | |
| private static final int elements = 5000; | |
| private static List<List<Double>> array01; | |
| private static double[][] array02; | |
| public static void main(String[] args) { | |
| StopWatch sw = new StopWatch(); | |
| // init | |
| array01 = new ArrayList<>(); | |
| for(int i = 0; i < elements; i++) { | |
| ArrayList<Double> array = new ArrayList<>(); | |
| for(int j = 0; j < elements; j++){ | |
| array.add(0D); | |
| } | |
| array01.add(array); | |
| } | |
| array02 = new double[elements][elements]; | |
| // ----------------------------------------- | |
| for(int i = 0; i < 10; i++){ | |
| sw.restart(); | |
| for(double j = 0; j < 100; j++) { | |
| // ArrayList | |
| test01(j); | |
| } | |
| sw.stop(); | |
| System.out.println(sw.getElapsed() + "ms"); | |
| } | |
| System.out.println(); | |
| for(int i = 0; i < 10; i++){ | |
| sw.restart(); | |
| for(double j = 0; j < 100; j++) { | |
| // array | |
| test02(j); | |
| // cached ArrayList | |
| //test03(j); | |
| } | |
| sw.stop(); | |
| System.out.println(sw.getElapsed() + "ms"); | |
| } | |
| } | |
| private static void test01(double x){ | |
| for(int i = 0; i < elements; i++){ | |
| for(int j = 0; j < elements; j++){ | |
| array01.get(i).set(j, x); | |
| } | |
| } | |
| } | |
| private static void test02(double x){ | |
| for(int i = 0; i < elements; i++){ | |
| for(int j = 0; j < elements; j++){ | |
| array02[i][j] = x; | |
| } | |
| } | |
| } | |
| private static void test03(double x){ | |
| for(int i = 0; i < elements; i++){ | |
| List<Double> array = array01.get(i); | |
| for(int j = 0; j < elements; j++){ | |
| array.set(j, x); | |
| } | |
| } | |
| } | |
| } | |
| class StopWatch{ | |
| private long start; | |
| private long end; | |
| private long elapsed; | |
| private ArrayList<Long> raps; | |
| public StopWatch(){ | |
| reset(); | |
| } | |
| public void start(){ | |
| start = System.currentTimeMillis(); | |
| } | |
| public void stop(){ | |
| end = System.currentTimeMillis(); | |
| long diff = end - start; | |
| raps.add(diff); | |
| elapsed += diff; | |
| } | |
| public void restart(){ | |
| reset(); | |
| start(); | |
| } | |
| public void reset(){ | |
| start = System.currentTimeMillis(); | |
| end = start; | |
| raps = new ArrayList<>(); | |
| elapsed = 0; | |
| } | |
| public long getElapsed(){ | |
| return elapsed; | |
| } | |
| public ArrayList<Long> getRaps(){ | |
| return (ArrayList<Long>)raps.clone(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| namespace SpeedTestCS | |
| { | |
| class Program | |
| { | |
| const int elements = 5000; | |
| static List<List<double>> array01 = new List<List<double>>(); | |
| static double[,] array02 = new double[elements, elements]; | |
| static void Main(string[] args) | |
| { | |
| var sw = new Stopwatch(); | |
| // init | |
| for (int i = 0; i < elements; i++) | |
| { | |
| var array = new List<double>(); | |
| for (int j = 0; j < elements; j++) | |
| { | |
| array.Add(i); | |
| } | |
| array01.Add(array); | |
| } | |
| // ----------------------------------------- | |
| // List | |
| for (int i = 0; i < 10; i++) | |
| { | |
| sw.Restart(); | |
| for (double j = 0; j < 100; j++) | |
| { | |
| Test01(j); | |
| } | |
| sw.Stop(); | |
| Console.WriteLine("{0}ms", sw.ElapsedMilliseconds); | |
| } | |
| Console.WriteLine(); | |
| // Array | |
| for (int i = 0; i < 10; i++) | |
| { | |
| sw.Restart(); | |
| for (double j = 0; j < 100; j++) | |
| { | |
| Test02(j); | |
| } | |
| sw.Stop(); | |
| Console.WriteLine("{0}ms", sw.ElapsedMilliseconds); | |
| } | |
| } | |
| static void Test01(double x) | |
| { | |
| for (int i = 0; i < elements; i++) | |
| { | |
| for (int j = 0; j < elements; j++) | |
| { | |
| array01[i][j] = x; | |
| } | |
| } | |
| } | |
| static void Test02(double x) | |
| { | |
| for (int i = 0; i < elements; i++) | |
| { | |
| for (int j = 0; j < elements; j++) | |
| { | |
| array02[i, j] = x; | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<chrono> | |
| #include<iostream> | |
| #include<vector> | |
| class StopWatch { | |
| private: | |
| std::chrono::time_point<std::chrono::system_clock> _start; | |
| std::chrono::time_point<std::chrono::system_clock> _end; | |
| long long _elapsed; | |
| std::vector<long long> _raps; | |
| public: | |
| StopWatch() { | |
| reset(); | |
| } | |
| void start() { | |
| _start = std::chrono::system_clock::now(); | |
| } | |
| void stop() { | |
| _end = std::chrono::system_clock::now(); | |
| auto diff = _end - _start; | |
| auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(diff); | |
| _raps.push_back(ms.count()); | |
| _elapsed += ms.count(); | |
| } | |
| void restart() { | |
| reset(); | |
| start(); | |
| } | |
| void reset() { | |
| _start = std::chrono::system_clock::now(); | |
| _end = _start; | |
| _raps = std::vector<long long>(); | |
| _elapsed = 0; | |
| } | |
| long long getElapsed() { | |
| return _elapsed; | |
| } | |
| std::vector<long long> getRaps() { | |
| return _raps; | |
| } | |
| }; | |
| const int elements = 5000; | |
| std::vector<std::vector<double>> array01; | |
| double array02[5000][5000]; | |
| void test01(double x); | |
| void test02(double x); | |
| void main() { | |
| auto sw = new StopWatch(); | |
| // init | |
| for (int i = 0; i < elements; i++) { | |
| std::vector<double> array; | |
| for (int j = 0; j < elements; j++) { | |
| array.push_back(i); | |
| } | |
| array01.push_back(array); | |
| } | |
| // ----------------------------------------- | |
| // Vector | |
| for (int i = 0; i < 10; i++) { | |
| sw->restart(); | |
| for (double j = 0; j < 100; j++) { | |
| test01(j); | |
| } | |
| sw->stop(); | |
| std::cout << sw->getElapsed() << "ms" << std::endl; | |
| } | |
| std::cout << std::endl; | |
| // Array | |
| for (int i = 0; i < 10; i++) { | |
| sw->restart(); | |
| for (double j = 0; j < 100; j++) { | |
| test02(j); | |
| } | |
| sw->stop(); | |
| std::cout << sw->getElapsed() << "ms" << std::endl; | |
| } | |
| } | |
| void test01(double x) { | |
| for (int i = 0; i < elements; i++) { | |
| for (int j = 0; j < elements; j++) { | |
| array01[i][j] = x; | |
| } | |
| } | |
| } | |
| void test02(double x) { | |
| for (int i = 0; i < elements; i++) { | |
| for (int j = 0; j < elements; j++) { | |
| array02[i][j] = x; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment