怎么使用 Javascript 内置函数记录每次挖矿所消耗的时间。最后对同一难度下的16次挖矿所消耗的时间进行统计计算其平均值。给出以下代码完整代码var difficulty = 4; number of zeros required at front of hashvar maximumNonce = 500000; limit the nonce to this so we
var difficulty = 4; // number of zeros required at front of hash var maximumNonce = 500000; // limit the nonce to this so we don't mine too long var times = []; // array to store mining times
// 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(-difficulty, 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) { var start = new Date().getTime(); //记录开始时间 for (var x = 0; x <= maximumNonce; x++) { $('#block' + block + 'chain' + chain + 'nonce').val(x); $('#block' + block + 'chain' + chain + 'hash').val(sha256(block, chain)); if ($('#block' + block + 'chain' + chain + 'hash').val().substr(-difficulty, difficulty) === pattern) { if (isChain) { updateChain(block, chain); } else { updateState(block, chain); } var end = new Date().getTime(); //记录结束时间 times.push(end - start); //将挖矿所消耗的时间加入数组 break; } } }
// mine 16 times and record the mining times for (var i = 0; i < 16; i++) { mine(1, i+1, true); }
// calculate the average mining time var sum = 0; for (var j = 0; j < times.length; j++) { sum += times[j]; } var averageTime = sum / times.length;
console.log('Average mining time for difficulty ' + difficulty + ': ' + averageTime + ' ms'); //输出平均挖矿时
原文地址: https://www.cveoy.top/t/topic/hwv2 著作权归作者所有。请勿转载和采集!