Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Created January 30, 2025 04:47
Show Gist options
  • Save Muqsit/d9885113f2e03fbe5c18496116a978f4 to your computer and use it in GitHub Desktop.
Save Muqsit/d9885113f2e03fbe5c18496116a978f4 to your computer and use it in GitHub Desktop.
NBT CompoundTag::equals vs. Equality operator (PHP)
<?php
declare(strict_types=1);
use pocketmine\nbt\BigEndianNbtSerializer;
$n_runs = 100;
// wget https://gist.githubusercontent.com/Muqsit/08391495f934f5c47be0f837ac2907ab/raw/6d36652ca8b557bbaf96115a846ed25a1e45e7c6/inventory.json
$inventory = file_get_contents("/home/cosmicpe/dev/plugin_data/Scripter/inventory.json");
$inventory = json_decode($inventory, true, 2, JSON_THROW_ON_ERROR);
$inventory = array_map(hex2bin(...), $inventory);
$serializer = new BigEndianNbtSerializer();
$inventory1 = array_map(fn($x) => $serializer->read($x)->mustGetCompoundTag(), $inventory);
$inventory2 = array_map(fn($x) => $serializer->read($x)->mustGetCompoundTag(), $inventory);
$results1 = [];
$t = hrtime(true);
for($i = 0; $i < $n_runs; $i++){
foreach($inventory1 as $index => $value){
$results1[] = $value->equals($inventory2[$index]);
}
}
$t = hrtime(true) - $t;
echo "equals(): ", number_format($t / 1_000_000, 3), "ms", PHP_EOL;
$results2 = [];
$t = hrtime(true);
for($i = 0; $i < $n_runs; $i++){
foreach($inventory1 as $index => $value){
$results2[] = $value == $inventory2[$index];
}
}
$t = hrtime(true) - $t;
echo "==: ", number_format($t / 1_000_000, 3), "ms", PHP_EOL;
echo "integrity test: ", $results1 === $results2 ? "succeeded" : "failed", PHP_EOL;
@Muqsit
Copy link
Author

Muqsit commented Jan 30, 2025

equals(): 495.906ms
==: 0.061ms
integrity test: succeeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment