解决方案
假设只有一行被标记,并且开头/Perspective:
的第一行是列标题行......\t
过度评论(为了清楚起见)
$perspectives = array(); //Initialise perspectives array
$columns = array(); //Initialise column names array
$text_file = fopen('./file', 'r'); //Open file to handle
while($line = fgets($text_file)){ //Read file line by line
if(strpos($line, '/Perspective:') === 0){ //Check if '/Perspective:' is at start of string
$perspectives = explode(' ', substr($line, 14)); // Remove first 14 characters: /Perspective:
continue;
}
else if(strpos($line, "\t") === 0){ //Check if first char in line is \t
$columns = explode("\t",
preg_replace("#\t/.+#", '', substr($line, 1)) //Remove commented column names and first \t
);
break; // Break while loop after column names row
}
}
未注释的代码
$perspectives = array();
$columns = array();
$text_file = fopen('./file', 'r');
while($line = fgets($text_file)){
if(strpos($line, '/Perspective:') === 0){
$perspectives = explode(' ', substr($line, 14));
continue;
}
elseif(strpos($line, "\t") === 0){
$columns = explode("\t",
preg_replace("#\t/.+#", '', substr($line, 1))
);
break;
}
}
输入文件
/Purpose: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam, suscipit incidunt doloribus voluptatum dicta maxime accusantium animi eum vero eaque odit quae non quaerat possimus enim ad numquam consequuntur beatae.
/Origin: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima, animi minus perspiciatis laudantium? Nostrum, aspernatur, sequi ratione assumenda fuga similique architecto deleniti sint recusandae voluptatibus numquam obcaecati ducimus eaque nisi.
/Rawdata: Unknown
/Perspective: skincover intelligence legs
/Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro, libero, accusamus laboriosam modi voluptatem facere quod unde atque perferendis laborum nisi omnis nihil cum minima quaerat. Quia, quaerat ipsa molestiae.
legs skincover weight intelligence speed /something /else
dog 1 1 1 1 1 1 1
pig 1 1 1 1 1 1 1
human 1 1 1 1 1 1 1
边注
出于好奇,我用您的代码(代码 A)与我的代码(代码 B )对比,看看哪个表现更好。
结果
执行时间:
Code A: 0.000108
Code B: 0.000044
代码 B的2.4545454545
速度要快几倍,并且可以执行整个操作perspecitves
,并且column names
如果没有过多地分析这两个代码,我建议造成差异的主要原因是我们处理文件的方式。
注意
我确实进行了多次比较,差异大致从2.2x
到2.7x
。
此外,时间都非常小,所以这不是什么大不了的事……