跳转至

@ohos.util.HashSet

@ohos.util.HashSet.entries

📖 官方API文档

⌨ 测试用例 1: 测试HashSet容器的entries接口,set长度为输入的loopCount

function test_entries(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < loopCount; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.entries();
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 0.001540 ms

@ohos.util.HashSet.forEach

📖 官方API文档

⌨ 测试用例 1: 测试HashSet容器的forEach接口,set长度为500000

function test_forEach(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < 500000; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.forEach((value?: number, key?: number) => {
    });
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 63.176667 ms

@ohos.util.HashSet.add

📖 官方API文档

⌨ 测试用例 1: 测试HashSet容器add接口

function test_add(loopCount: number) {
  let set = new HashSet<number>();
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.add(1);
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 0.000288 ms

@ohos.util.HashSet.remove

📖 官方API文档

⌨ 测试用例 1: 测试HashSet容器remove接口

function test_remove(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < loopCount; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.remove(i);
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 0.000138 ms

@ohos.util.HashSet.values

📖 官方API文档

⌨ 测试用例 1: 测试HashSet容器的values接口,set长度为输入的loopCount

function test_values(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < loopCount; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.values();
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 0.000930 ms

@ohos.util.HashSet.has

📖 官方API文档

⌨ 测试用例 1: 测试在最坏情况下(查找元素不存在的情况)HashSet容器的has接口,set长度为500000

function test_has(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < 500000; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.has(500000);
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
⌨ 测试用例 2: 测试在最优情况下(查找元素为第一个)HashSet容器的has接口,set长度为5000000

function test_has2(loopCount: number) {
  let set = new HashSet<number>();
  for (let i = 0; i < 500000; i++) {
    set.add(i);
  }
  const startTime = new Date();
  for (let i = 0; i < loopCount; i++) {
    set.has(0);
  }
  const endTime = new Date();
  const executionTime = endTime.getTime() - startTime.getTime();
  const averageTime = executionTime / loopCount;
  console.log(`!${test_id}: ${averageTime} ms`);
}
🕹 测试结果

测试用例 测试平台 运行时间
测试用例 1 Huawei Phone 0.000118 ms
测试用例 2 Huawei Phone 0.000070 ms