让我们将以下代码段视为示例:
var len = 1000000,
testArr = []
for (var i = 0; i < len; i++) {
testArr.push(i+1)
}
function mprofile(name, subject, object) {
var start = new Date().getTime(),
result = subject(object),
end = new Date().getTime() - start
console.log(name)
console.log('Result: ' + result)
console.log(end)
}
var length = testArr.length,
start = new Date().getTime(),
cnt = 0
for (i = 0; i < length; i++) {
cnt += testArr[i]
}
console.log('Regular loop:')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
for (i = length; i--; ) {
cnt += testArr[i]
}
console.log('Reversered loop')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
var startAt = length%8,
iterations = Math.floor((length+7) / 8)
do {
switch (startAt) {
case 0: cnt += testArr[i++]
case 7: cnt += testArr[i++]
case 6: cnt += testArr[i++]
case 5: cnt += testArr[i++]
case 4: cnt += testArr[i++]
case 3: cnt += testArr[i++]
case 2: cnt += testArr[i++]
case 1: cnt += testArr[i++]
}
startAt = 0
} while(--iterations)
console.log('Duffs device')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
start = new Date().getTime()
cnt = i = 0
iterations = Math.floor((length+7) / 8)
switch (length % 8) {
case 0: cnt += testArr[i++]
case 7: cnt += testArr[i++]
case 6: cnt += testArr[i++]
case 5: cnt += testArr[i++]
case 4: cnt += testArr[i++]
case 3: cnt += testArr[i++]
case 2: cnt += testArr[i++]
case 1: cnt += testArr[i++]
}
while(--iterations) {
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
cnt += testArr[i++]
}
console.log('Optimized Duffs device')
console.log('Result: ' + cnt)
console.log(new Date().getTime() - start);
mprofile(
'Profiled regular loop',
function(arr) {
var cnt = 0,
length = arr.length
for (i = 0; i < length; i++) {
cnt += testArr[i]
}
return cnt
},
testArr
)
mprofile(
'Profiled reversed loop',
function(arr) {
var cnt = 0,
length = arr.length
for (i = length; i--; ) {
cnt += testArr[i]
}
return cnt
},
testArr
)
mprofile(
'Profiled Duffs device',
function(arr) {
var cnt = i = 0,
length = arr.length,
startAt = length%8,
iterations = Math.floor((length+7) / 8)
do {
switch (startAt) {
case 0: cnt += arr[i++]
case 7: cnt += arr[i++]
case 6: cnt += arr[i++]
case 5: cnt += arr[i++]
case 4: cnt += arr[i++]
case 3: cnt+ = arr[i++]
case 2: cnt += arr[i++]
case 1: cnt += arr[i++]
}
startAt = 0
} while(--iterations)
return cnt
},
testArr
)
mprofile(
'Profiled optimized Duffs device',
function(arr) {
var cnt = i = 0,
length = arr.length,
iterations = Math.floor((length+7) / 8)
switch (length % 8) {
case 0: cnt += arr[i++]
case 7: cnt += arr[i++]
case 6: cnt += arr[i++]
case 5: cnt += arr[i++]
case 4: cnt += arr[i++]
case 3: cnt += arr[i++]
case 2: cnt += arr[i++]
case 1: cnt += arr[i++]
}
while(--iterations) {
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
cnt += arr[i++]
}
return cnt
},
testArr
)
从普通循环报告的执行时间与回调内执行的循环之间存在差异。另外,如果您在头部脚本标记内部执行此操作,则执行时间会有所不同,请执行开发人员控制台,如下图所示:
答案 0 :(得分:10)
$userid= $_POST['userid'];
$getpassword= $_POST['password'];
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT userid,password FROM aster_users WHERE userid = '$userid' AND password = '$getpassword'";
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
if($count==1){
echo "login successfully";
} else {
echo "Login failed" . mysqli_error($conn);
}
是衡量效果的最佳选择。https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
结束时间计算错误。在performance.now()
函数中,您在调用第一个mprofile
之前计算差异,在普通代码中 - 在第二个之后。因此,在一种情况下,您将控制台与控制台的交互包含在其他测量中,而不是
此外,整个示例有点不正确。您应该创建console.log
,loop
等函数,并在调用它们之前/之后测量时间。那么你应该在回调中测量相同功能的时间。并且你应该每个案例运行至少10次并检查平均时间。
答案 1 :(得分:3)
如果您使用Chrome console.time()
非常有用。
e.g:
console.time('myTime1')
console.timeEnd('myTime1') //myTime1: 5047.492ms