我想写一个记录器(请不要评论为什么和"使用......") 但是我对nodejs(event?)loop / forEach感到困惑。
例如:
for(var i = 0; i<100; i++){
process.stdout.write(Date.now().toString() + "\n", "utf8");
};
&#13;
输出为:1466021578453,1466021578453,1466021578469,1466021578469
问题:延迟从16ms开始;我该如何防止这种情况?
编辑:Windows 7,x64; (延迟Ubuntu 15,最长2毫秒)
答案 0 :(得分:1)
sudo ltrace -o outlog node myTest.js
这可能比您想要的更多。我的计算机Date.now()
上的clock_gettime
转换为clock_gettime
。您想查看后续ltrace
调用之间的内容。你也写到STDOUT,每次你这样做都有开销。您可以在% time seconds usecs/call calls function
------ ----------- ----------- --------- --------------------
28.45 6.629315 209 31690 memcpy
26.69 6.219529 217 28544 memcmp
16.78 3.910686 217 17990 free
9.73 2.266705 214 10590 malloc
2.92 0.679971 220 3083 _Znam
2.86 0.666421 216 3082 _ZdaPv
2.55 0.593798 206 2880 _ZdlPv
2.16 0.502644 211 2378 _Znwm
1.09 0.255114 213 1196 strlen
0.69 0.161741 215 750 pthread_getspecific
0.67 0.155609 209 744 memmove
0.57 0.133857 212 631 _ZNSo6sentryC1ERSo
0.57 0.133344 226 589 pthread_mutex_lock
0.52 0.121342 206 589 pthread_mutex_unlock
0.46 0.106343 207 512 clock_gettime
0.40 0.093022 204 454 memset
0.39 0.089857 216 416 _ZNSt9basic_iosIcSt11char_traitsIcEE4initEPSt15basic_streambufIcS1_E
0.22 0.050741 195 259 strcmp
0.20 0.047454 228 208 _ZNSt8ios_baseC2Ev
0.20 0.047236 227 208 floor
0.19 0.044603 214 208 _ZNSt6localeC1Ev
0.19 0.044536 212 210 _ZNSs4_Rep10_M_destroyERKSaIcE
0.19 0.044200 212 208 _ZNSt8ios_baseD2Ev
下运行整个过程,看看发生了什么,并使用-c获取摘要。
对我来说,它在没有在ltrace下运行时运行3毫秒。
clock_gettime
我不确定为什么那里有31,690个memcpy和28544个memcmp。这看起来有点过分,但也许只是JIT启动成本,就运行时成本而言,你可以看到有{512}次调用clock_gettime
。也不知道为什么会有那么多电话,但你可以在//this works for me ===>
<?php $this->PhpExcel->createWorksheet()
->setDefaultFont('Calibri', 12);
$this->PhpExcel->createSheet();
$this->PhpExcel->setActiveSheetIndex(0);
$this->PhpExcel->setWorksheetName("System Unit/Laptop");
$table = array(
array('label' => __('Category'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Item Name'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('PC-Name'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Serial #'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('CPU'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Status'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Motherboard'),'width' => 'auto','wrap' => 'true','filter' => true),
array('label' => __('Processor'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Memory'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Video Card'),'width' => 'auto','wrap' => 'true','filter' => true),
array('label' => __('HDD'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('OS'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Product key'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Location'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Department'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('User'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Remarks'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Comment'),'wrap' => 'true','width' => 'auto', 'filter' => true),
);
$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
foreach ($data as $item) {
if ($item['Category']['category_id'] == '1' || $item['Category']['category_id'] == '3'){
$this->PhpExcel->addTableRow(array(
$item['Category']['category_name'] ,
$item['Item']['item_property_number'],
$item['Item']['item_name'],
$item['Item']['item_serial_number'],
$item['Item']['item_cpu'],
$item['Item']['item_model'],
$item['Item']['item_mom'],
$item['Item']['item_processor'],
$item['Item']['item_memory'],
$item['Item']['item_vidcard'],
$item['Item']['item_su_hd'],
$item['Item']['item_su_os'],
$item['Item']['item_key'],
$item['Item']['item_floor'],
$item['Item']['item_dept'],
$item['Employee']['employee_lastname'],
$item['Item']['item_status'] ? 'Serviceable' : 'Unusable',
$item['Item']['item_comment']
));
}
}
$this->PhpExcel->addTableFooter();
$this->PhpExcel->createSheet();
$this->PhpExcel->setActiveSheetIndex(01);
$this->PhpExcel->setWorksheetName("Others");
$this->PhpExcel->setRow(1);
$table2 = array(
array('label' => __('Category'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Item Name'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('PC-Name'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Serial #'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Location'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Department'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('User'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Remarks'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Comment'),'width' => 'auto','wrap' => 'true', 'filter' => true),
);
$this->PhpExcel->addTableHeader($table2, array('name' => 'Cambria', 'bold' => true));
foreach ($data as $item) {
if ($item['Category']['category_id'] == '2' || $item['Category']['category_id'] >= '4'){
$this->PhpExcel->addTableRow(array(
$item['Category']['category_name'],
$item['Item']['item_property_number'],
$item['Item']['item_name2'],
$item['Item']['item_serial_number'],
$item['Item']['item_floor'],
$item['Item']['item_dept'],
$item['Employee']['employee_lastname'],
$item['Item']['item_status'] ? 'Serviceable' : 'Unusable',
$item['Item']['item_comment']
));
}
}
$this->PhpExcel->addTableFooter();
$this->PhpExcel->createSheet();
$this->PhpExcel->setActiveSheetIndex(02);
$this->PhpExcel->setWorksheetName("Repair History");
$this->PhpExcel->setRow(1);
$table = array(
array('label' => __('Repair ID'),'wrap' => 'true','width' => 'auto', 'filter' => 'Monitor'),
array('label' => __('Repair Item'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Pre-Date'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Local #'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Pre-Findings'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Pre-Recom'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Pre Eu Rep'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Pre It Rep'),'wrap' => 'true','width' => 'auto', 'filter' => true),
array('label' => __('Repair Pre Admin Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post Date'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post Findings'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post Recom'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post Eu Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post It Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Post Admin Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Manner'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Date Pullout'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Date Returned'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Item Condition'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Cost'),'width' => 'auto','wrap' => 'true', 'filter' => true),
array('label' => __('Repair Status'),'width' => 'auto','wrap' => 'true', 'filter' => true),
);
$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
foreach ($itemRepair as $item) {
$this->PhpExcel->addTableRow(array(
$item['Repair']['repair_id'],
$item['Item']['item_property_number'],
$item['Repair']['repair_pre_date'],
$item['Repair']['repair_local_no'],
$item['Repair']['repair_pre_findings'],
$item['Repair']['repair_pre_recom'],
$item['Repair']['repair_pre_eu_rep'],
$item['Repair']['repair_pre_it_rep'],
$item['Repair']['repair_pre_admin_rep'],
$item['Repair']['repair_post_date'],
$item['Repair']['repair_post_findings'],
$item['Repair']['repair_post_recom'],
$item['Repair']['repair_post_eu_rep'],
$item['Repair']['repair_post_it_rep'],
$item['Repair']['repair_post_admin_rep'],
$item['Repair']['repair_manner']? 'In-house': 'Outsource' ,
$item['Repair']['repair_date_pullout'],
$item['Repair']['repair_date_returned'],
$item['Repair']['repair_item_condition'],
$item['Repair']['repair_cost'],
$item['Repair']['repair_status'] ? 'Serviceable':'Unusable' ,
));
}
$this->PhpExcel->addTableFooter()
->output();
?>
看到丢失了106ms。祝你好运。