Skip to content

Instantly share code, notes, and snippets.

@a2468834
Last active April 18, 2022 07:45
Show Gist options
  • Save a2468834/d1495a43039c9225fd822e644e12867f to your computer and use it in GitHub Desktop.
Save a2468834/d1495a43039c9225fd822e644e12867f to your computer and use it in GitHub Desktop.
// Main function
async function main() {
// Declare Signer object
const HRE_EOAs = await hre.ethers.getSigners();
// Deploy contracts
const factory_seq = await hre.ethers.getContractFactory("SortedSequence", HRE_EOAs[0]);
const contract_seq = await factory_seq.deploy();
await contract_seq.deployed();
const factory_bad = await hre.ethers.getContractFactory("BadSearch", HRE_EOAs[0]);
const contract_bad = await factory_bad.deploy();
await contract_bad.deployed();
const factory_okay = await hre.ethers.getContractFactory("OkaySearch", HRE_EOAs[0]);
const contract_okay = await factory_okay.deploy();
await contract_okay.deployed();
// Initialize state variables in 'SortedSequence'
var args = [[
257, 448, 605, 716, 735, 916, 1006, 1041, 1050, 1094,
1103, 1151, 1152, 1180, 1182, 1302, 1358, 1371, 1571, 1657,
1952, 2448, 2480, 2618, 2644, 2913, 3018, 3091, 3109, 3316,
3416, 3603, 3723, 3859, 3908, 3949, 4228, 4250, 4320, 4327,
4388, 4400, 4440, 4552, 4675, 4704, 4895, 4970, 5357, 5567,
5800, 5812, 5916, 5986, 6146, 6200, 6241, 6260, 6297, 6411,
6520, 6564, 6746, 6850, 6859, 6976, 6982, 7066, 7127, 7461,
7511, 7543, 7773, 7803, 7807, 7809, 7860, 7891, 7897, 8093,
8369, 8397, 8444, 8504, 8779, 8881, 9111, 9132, 9193, 9318,
9345, 9405, 9502, 9582, 9591, 9603, 9812, 9854, 9866, 9958]];
await (await contract_seq.setValue(...args)).wait();
console.log("\nFind '0': expected return = FALSE");
console.log("--------------------");
console.log("Linear: ", (await contract_bad.estimateGas.linSearch(0)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Binary: ", (await contract_bad.estimateGas.binSearch(0)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Mapping:", (await contract_okay.estimateGas.askMapping(0)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("\nFind '6200': expected return = TRUE");
console.log("--------------------");
console.log("Linear: ", (await contract_bad.estimateGas.linSearch(6200)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Binary: ", (await contract_bad.estimateGas.binSearch(6200)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Mapping:", (await contract_okay.estimateGas.askMapping(6200)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("\nFind '9959': expected return = FALSE");
console.log("--------------------");
console.log("Linear: ", (await contract_bad.estimateGas.linSearch(9959)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Binary: ", (await contract_bad.estimateGas.binSearch(9959)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
console.log("Mapping:", (await contract_okay.estimateGas.askMapping(9959)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
}
main()
.then(() => {
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});
@a2468834
Copy link
Author

a2468834 commented Apr 17, 2022

Terminal output

$ yarn hardhat run scripts/search.js
yarn run v1.22.18
Compiled 1 Solidity file successfully

Find '0': expected return = FALSE
--------------------
Linear:  24,063
Binary:  26,738
Mapping: 23,992

Find '6200': expected return = TRUE
--------------------
Linear:  255,323
Binary:  44,795
Mapping: 24,016

Find '9959': expected return = FALSE
--------------------
Linear:  255,323
Binary:  44,795
Mapping: 24,016

@a2468834
Copy link
Author

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