使用 JavaScript 内置函数记录和统计挖矿时间
使用 JavaScript 内置函数记录和统计挖矿时间
本文提供完整代码示例,展示如何在 JavaScript 中使用内置函数记录每次挖矿所消耗的时间,并对同一难度下的 16 次挖矿时间进行统计和计算平均值。
var difficulty = 4; // number of zeros required at front of hash
var maximumNonce = 50000000; // limit the nonce to this so we don't mine too long
// NOTE: Because there are 16 possible characters in a hex value, each time you increment
// the difficulty by one you make the puzzle 16 times harder. In my testing, a difficulty
// of 6 requires a maximumNonce well over 500,000,000.
/////////////////////////
// global variable setup
/////////////////////////
var pattern = '';
for (var x = 0; x < difficulty; x++) {
pattern += '0';
}
/////////////////////////
// functions
/////////////////////////
function sha256(block, chain) {
// calculate a SHA256 hash of the contents of the block
return CryptoJS.SHA256(getText(block, chain));
}
function updateState(block, chain) {
// set the well background red or green for this block
if ($('#block' + block + 'chain' + chain + 'hash').val().substr(0, difficulty) === pattern) {
$('#block' + block + 'chain' + chain + 'well').removeClass('well-error').addClass('well-success');
}
else {
$('#block' + block + 'chain' + chain + 'well').removeClass('well-success').addClass('well-error');
}
}
function updateHash(block, chain) {
// update the SHA256 hash value for this block
$('#block' + block + 'chain' + chain + 'hash').val(sha256(block, chain));
updateState(block, chain);
}
function updateChain(block, chain) {
// update all blocks walking the chain from this block to the end
for (var x = block; x <= 5; x++) {
if (x > 1) {
$('#block' + x + 'chain' + chain + 'previous').val($('#block' + (x - 1).toString() + 'chain' + chain + 'hash').val());
}
updateHash(x, chain);
}
}
function mine(block, chain, isChain) {
for (var x = 0; x <= maximumNonce; x++) {
$('#block' + block + 'chain' + chain + 'nonce').val(x);
const startTime = performance.now();
$('#block' + block + 'chain' + chain + 'hash').val(sha256(block, chain));
const finishTime = performance.now();
console.log('Time taken to mine block ' + block + ' in chain ' + chain + ': ' + Math.floor(finishTime - startTime) + ' milliseconds.');
if ($('#block' + block + 'chain' + chain + 'hash').val().substr(0, difficulty) === pattern) {
if (isChain) {
updateChain(block, chain);
}
else {
updateState(block, chain);
}
break;
}
}
}
// mine 16 blocks in chain 1 and record the time taken for each block
const blockTimes = [];
for (let i = 0; i < 16; i++) {
const startTime = performance.now();
mine(1, i, true);
const finishTime = performance.now();
blockTimes.push(finishTime - startTime);
}
// calculate the average time taken for all 16 blocks
var totalTime = 0;
for (let i = 0; i < 16; i++) {
totalTime += blockTimes[i];
}
var averageTime = totalTime / 16;
console.log('Average time taken to mine a block: ' + averageTime + ' milliseconds.');
需要修改的文件只有一个,就是在头部引入 CryptoJS 库的文件。如果这个库还没有被引入,需要在头部加入以下代码:
<script src='https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha256.js'></script>
代码说明:
- 使用
performance.now()函数记录挖矿开始和结束的时间。 - 使用
Math.floor()函数将时间转换为毫秒并进行格式化输出。 - 使用数组
blockTimes记录每次挖矿的耗时。 - 使用循环计算所有挖矿时间的总和,并计算平均值。
注意:
- 此代码示例中的
getText(block, chain)函数需要根据实际情况进行替换,用于获取需要进行哈希运算的文本内容。 - 由于挖矿难度会影响挖矿时间,实际运行中可能需要调整
difficulty和maximumNonce的值。 - 为了方便展示,代码示例中直接在控制台中输出时间信息,实际应用中可以根据需要将时间信息展示在页面上。
原文地址: https://www.cveoy.top/t/topic/f22F 著作权归作者所有。请勿转载和采集!