22 July 2016

So sánh tốc độ chạy của Vector và ArrayList trong Java

Vector - ArrayList "currentTimeMillis"
import java.util.*;

public class CollectionsThreadSafeTest {

    public void testVector() {
        long startTime = System.currentTimeMillis();
        Vector<Integer> vector = new Vector<>();
        for (int i = 0; i < 10_000_000; i++) {
            vector.addElement(i);
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println("Test Vector: " + totalTime + " ms");
    }
    public void testArrayList() {
        long startTime = System.currentTimeMillis();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10_000_000; i++) {
            list.add(i);
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println("Test ArrayList: " + totalTime + " ms");
    }
    public static void main(String[] args) {
        CollectionsThreadSafeTest tester = new CollectionsThreadSafeTest();
        tester.testVector();
        tester.testArrayList();
    }
}
1
2
Test Vector: 9266 ms
Test ArrayList: 4588 ms

Related Posts:

  • Java: Method Radom String - Reset radom password Radom method Java 2016 package com.giaima; import java.security.SecureRandom; public class RadomString { static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static Sec… Read More
  • Convert Date ngày tháng năm để set kiểu khai báo Date trong Java Convert kiểu Date ngày tháng năm để set kiểu khai báo Date Date date = new Date(); SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd"); String ngay = formatDate.format(date); Date ngayDate = formatDate.parse(n… Read More
  • How to Write / Insert Image Into Mysql Database Using Java TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB A binary large object that can hold a variable amount of binary data. The sorting and comparison of the values for these objects is performed in case-sensitive manner. TINYBLOB A b… Read More
  • JDBC ===JDBC=== 1- Register JDBC driver 2- Open a connection 3- Execute a query 4- Extract data from result set 5- close ======================================================= 1: cai dat mysql full co workbench 2: kiem tra xem … Read More
  • Tính đóng gói trong Java (Encapsulation) Encapsulation là gì? Tính bao đóng trong Java là một tiến trình đóng gói code và dữ liệu lại với nhau vào trong một đơn vị unit đơn, ví dụ như một gói bột giặt là hỗn hợp của các hạt bột giặt khác nhau. Chúng ta có thể tạo … Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang