Writing a ~100Kb HTML string over an MD file (number of iterations ~10K)
$begingroup$
I have tried to write a large string (~100-120 Kb of HTML) on an md
file, and am pretty sure it's not the fastest method, even though it only has to iterate ~8000-10,000 times and few times per hour.
There is also a low (~1%-2%) probability that the target filename has an old name (previousName
), not exactly matched with a new name (newName
), because the data flows through an API.
Key Script: Inside For
Loop
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // slug company
$hay=strtolower($arr["quote"]["primaryExchange"]); // exchange market
if(strpos($hay, 'nasdaq')===0){
$mk='nasdaq-us';
$nasdaq++;
}elseif(strpos($hay, 'nyse')===0 || strpos($hay, 'new york')===0){
$mk='nyse-us';
$nyse++;
}elseif(strpos($hay, 'cboe')===0){
$mk='cboe-us';
$cboe++;
}else{
$mk='market-us';
$others++;
}
$sc=str_replace(array(' '), array('-'), strtolower($s["quote"]["sector"])); // slug sector
$enc=UpdateStocks::getEnc($symb,$symb,$symb,self::START_POINT_URL_ENCRYPTION_APPEND, self::LENGTH_URL_ENCRYPTION_APPEND); // simple 4 length encryption output: e.g., 159a
$dir=__DIR__ . self::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
if(!is_dir($dir)){mkdir($dir, 0755,true);} // creates price targets directory if not exist
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
// new md filename
$newName=$dir . self::SLASH . $lurl . self::EXTENSION_MD;
// Replace multiple dashes with single dash: "aa-alcoa-basic-materials-nyse-us-159a"
$newName = preg_replace('/-{2,}/', '-', $newName);
// if file not exist: generate file
if($previousNames==null){
$fh=fopen($newName, 'wb');
fwrite($fh, '');
fclose($fh);
}else{
// if file not exist:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
}
// This method is not review required now.
$mdFileContent=UpdateStocks::getBaseHTML($s,$l,$z); // gets HTML
if(file_exists($newName)){
if(is_writable($newName)){
file_put_contents($newName,$mdFileContent);
echo $symb. " 💚 " . self::NEW_LINE;
}else{
echo $symb . " symbol file in front directory is not writable in " . __METHOD__ . " 💔" . self::NEW_LINE;
}
}else{
echo $symb . " file not found in " . __METHOD__ . " 💛" . self::NEW_LINE;
}
}
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
var_dump($hay)
string(20) "nasdaq global"
string(23) "new york stock exchange"
string(20) "nasdaq global market"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "nyse arca"
string(23) "new york stock exchange"
string(23) "nyse"
string(20) "nasdaq"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "cboe"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "new york stock exchange"
...
>5% Probability of var_dump($lurl)
For strtolower($symb)===strtolower($cn)
string(27) "aac-healthcare-nyse-us-e92a"
string(35) "aaon-basic-materials-nasdaq-us-238e"
string(28) "abb-industrials-nyse-us-a407"
string(38) "acnb-financial-services-nasdaq-us-19fa"
<95% Probability of var_dump($lurl)
For not strtolower($symb)===strtolower($cn)
string(50) "aadr-advisorshares-dorsey-wright-adr--nyse-us-d842"
string(39) "aal-airlines-industrials-nasdaq-us-29eb"
string(68) "aamc-altisource-asset-management-com-financial-services-nyse-us-b46a"
string(47) "aame-atlantic-financial-services-nasdaq-us-8944"
string(35) "aan-aarons-industrials-nyse-us-d00e"
string(54) "aaoi-applied-optoelectronics-technology-nasdaq-us-1dee"
string(56) "aap-advance-auto-parts-wi-consumer-cyclical-nyse-us-1f60"
string(36) "aapl-apple-technology-nasdaq-us-8f4c"
string(35) "aat-assets-real-estate-nyse-us-3598"
string(49) "aau-almaden-minerals-basic-materials-nyse-us-1c57"
string(51) "aaww-atlas-air-worldwide-industrials-nasdaq-us-69f3"
string(59) "aaxj-ishares-msci-all-country-asia-ex-japan--nasdaq-us-c6c4"
string(47) "aaxn-axon-enterprise-industrials-nasdaq-us-0eef"
string(58) "ab-alliancebernstein-units-financial-services-nyse-us-deb1"
Would you be so kind and help me to modify it with a faster/simpler script?
performance beginner php strings file-system
$endgroup$
add a comment |
$begingroup$
I have tried to write a large string (~100-120 Kb of HTML) on an md
file, and am pretty sure it's not the fastest method, even though it only has to iterate ~8000-10,000 times and few times per hour.
There is also a low (~1%-2%) probability that the target filename has an old name (previousName
), not exactly matched with a new name (newName
), because the data flows through an API.
Key Script: Inside For
Loop
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // slug company
$hay=strtolower($arr["quote"]["primaryExchange"]); // exchange market
if(strpos($hay, 'nasdaq')===0){
$mk='nasdaq-us';
$nasdaq++;
}elseif(strpos($hay, 'nyse')===0 || strpos($hay, 'new york')===0){
$mk='nyse-us';
$nyse++;
}elseif(strpos($hay, 'cboe')===0){
$mk='cboe-us';
$cboe++;
}else{
$mk='market-us';
$others++;
}
$sc=str_replace(array(' '), array('-'), strtolower($s["quote"]["sector"])); // slug sector
$enc=UpdateStocks::getEnc($symb,$symb,$symb,self::START_POINT_URL_ENCRYPTION_APPEND, self::LENGTH_URL_ENCRYPTION_APPEND); // simple 4 length encryption output: e.g., 159a
$dir=__DIR__ . self::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
if(!is_dir($dir)){mkdir($dir, 0755,true);} // creates price targets directory if not exist
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
// new md filename
$newName=$dir . self::SLASH . $lurl . self::EXTENSION_MD;
// Replace multiple dashes with single dash: "aa-alcoa-basic-materials-nyse-us-159a"
$newName = preg_replace('/-{2,}/', '-', $newName);
// if file not exist: generate file
if($previousNames==null){
$fh=fopen($newName, 'wb');
fwrite($fh, '');
fclose($fh);
}else{
// if file not exist:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
}
// This method is not review required now.
$mdFileContent=UpdateStocks::getBaseHTML($s,$l,$z); // gets HTML
if(file_exists($newName)){
if(is_writable($newName)){
file_put_contents($newName,$mdFileContent);
echo $symb. " 💚 " . self::NEW_LINE;
}else{
echo $symb . " symbol file in front directory is not writable in " . __METHOD__ . " 💔" . self::NEW_LINE;
}
}else{
echo $symb . " file not found in " . __METHOD__ . " 💛" . self::NEW_LINE;
}
}
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
var_dump($hay)
string(20) "nasdaq global"
string(23) "new york stock exchange"
string(20) "nasdaq global market"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "nyse arca"
string(23) "new york stock exchange"
string(23) "nyse"
string(20) "nasdaq"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "cboe"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "new york stock exchange"
...
>5% Probability of var_dump($lurl)
For strtolower($symb)===strtolower($cn)
string(27) "aac-healthcare-nyse-us-e92a"
string(35) "aaon-basic-materials-nasdaq-us-238e"
string(28) "abb-industrials-nyse-us-a407"
string(38) "acnb-financial-services-nasdaq-us-19fa"
<95% Probability of var_dump($lurl)
For not strtolower($symb)===strtolower($cn)
string(50) "aadr-advisorshares-dorsey-wright-adr--nyse-us-d842"
string(39) "aal-airlines-industrials-nasdaq-us-29eb"
string(68) "aamc-altisource-asset-management-com-financial-services-nyse-us-b46a"
string(47) "aame-atlantic-financial-services-nasdaq-us-8944"
string(35) "aan-aarons-industrials-nyse-us-d00e"
string(54) "aaoi-applied-optoelectronics-technology-nasdaq-us-1dee"
string(56) "aap-advance-auto-parts-wi-consumer-cyclical-nyse-us-1f60"
string(36) "aapl-apple-technology-nasdaq-us-8f4c"
string(35) "aat-assets-real-estate-nyse-us-3598"
string(49) "aau-almaden-minerals-basic-materials-nyse-us-1c57"
string(51) "aaww-atlas-air-worldwide-industrials-nasdaq-us-69f3"
string(59) "aaxj-ishares-msci-all-country-asia-ex-japan--nasdaq-us-c6c4"
string(47) "aaxn-axon-enterprise-industrials-nasdaq-us-0eef"
string(58) "ab-alliancebernstein-units-financial-services-nyse-us-deb1"
Would you be so kind and help me to modify it with a faster/simpler script?
performance beginner php strings file-system
$endgroup$
1
$begingroup$
You could dosubstr($hey, 0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what$hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
$endgroup$
– ArtisticPhoenix
9 hours ago
add a comment |
$begingroup$
I have tried to write a large string (~100-120 Kb of HTML) on an md
file, and am pretty sure it's not the fastest method, even though it only has to iterate ~8000-10,000 times and few times per hour.
There is also a low (~1%-2%) probability that the target filename has an old name (previousName
), not exactly matched with a new name (newName
), because the data flows through an API.
Key Script: Inside For
Loop
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // slug company
$hay=strtolower($arr["quote"]["primaryExchange"]); // exchange market
if(strpos($hay, 'nasdaq')===0){
$mk='nasdaq-us';
$nasdaq++;
}elseif(strpos($hay, 'nyse')===0 || strpos($hay, 'new york')===0){
$mk='nyse-us';
$nyse++;
}elseif(strpos($hay, 'cboe')===0){
$mk='cboe-us';
$cboe++;
}else{
$mk='market-us';
$others++;
}
$sc=str_replace(array(' '), array('-'), strtolower($s["quote"]["sector"])); // slug sector
$enc=UpdateStocks::getEnc($symb,$symb,$symb,self::START_POINT_URL_ENCRYPTION_APPEND, self::LENGTH_URL_ENCRYPTION_APPEND); // simple 4 length encryption output: e.g., 159a
$dir=__DIR__ . self::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
if(!is_dir($dir)){mkdir($dir, 0755,true);} // creates price targets directory if not exist
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
// new md filename
$newName=$dir . self::SLASH . $lurl . self::EXTENSION_MD;
// Replace multiple dashes with single dash: "aa-alcoa-basic-materials-nyse-us-159a"
$newName = preg_replace('/-{2,}/', '-', $newName);
// if file not exist: generate file
if($previousNames==null){
$fh=fopen($newName, 'wb');
fwrite($fh, '');
fclose($fh);
}else{
// if file not exist:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
}
// This method is not review required now.
$mdFileContent=UpdateStocks::getBaseHTML($s,$l,$z); // gets HTML
if(file_exists($newName)){
if(is_writable($newName)){
file_put_contents($newName,$mdFileContent);
echo $symb. " 💚 " . self::NEW_LINE;
}else{
echo $symb . " symbol file in front directory is not writable in " . __METHOD__ . " 💔" . self::NEW_LINE;
}
}else{
echo $symb . " file not found in " . __METHOD__ . " 💛" . self::NEW_LINE;
}
}
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
var_dump($hay)
string(20) "nasdaq global"
string(23) "new york stock exchange"
string(20) "nasdaq global market"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "nyse arca"
string(23) "new york stock exchange"
string(23) "nyse"
string(20) "nasdaq"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "cboe"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "new york stock exchange"
...
>5% Probability of var_dump($lurl)
For strtolower($symb)===strtolower($cn)
string(27) "aac-healthcare-nyse-us-e92a"
string(35) "aaon-basic-materials-nasdaq-us-238e"
string(28) "abb-industrials-nyse-us-a407"
string(38) "acnb-financial-services-nasdaq-us-19fa"
<95% Probability of var_dump($lurl)
For not strtolower($symb)===strtolower($cn)
string(50) "aadr-advisorshares-dorsey-wright-adr--nyse-us-d842"
string(39) "aal-airlines-industrials-nasdaq-us-29eb"
string(68) "aamc-altisource-asset-management-com-financial-services-nyse-us-b46a"
string(47) "aame-atlantic-financial-services-nasdaq-us-8944"
string(35) "aan-aarons-industrials-nyse-us-d00e"
string(54) "aaoi-applied-optoelectronics-technology-nasdaq-us-1dee"
string(56) "aap-advance-auto-parts-wi-consumer-cyclical-nyse-us-1f60"
string(36) "aapl-apple-technology-nasdaq-us-8f4c"
string(35) "aat-assets-real-estate-nyse-us-3598"
string(49) "aau-almaden-minerals-basic-materials-nyse-us-1c57"
string(51) "aaww-atlas-air-worldwide-industrials-nasdaq-us-69f3"
string(59) "aaxj-ishares-msci-all-country-asia-ex-japan--nasdaq-us-c6c4"
string(47) "aaxn-axon-enterprise-industrials-nasdaq-us-0eef"
string(58) "ab-alliancebernstein-units-financial-services-nyse-us-deb1"
Would you be so kind and help me to modify it with a faster/simpler script?
performance beginner php strings file-system
$endgroup$
I have tried to write a large string (~100-120 Kb of HTML) on an md
file, and am pretty sure it's not the fastest method, even though it only has to iterate ~8000-10,000 times and few times per hour.
There is also a low (~1%-2%) probability that the target filename has an old name (previousName
), not exactly matched with a new name (newName
), because the data flows through an API.
Key Script: Inside For
Loop
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // slug company
$hay=strtolower($arr["quote"]["primaryExchange"]); // exchange market
if(strpos($hay, 'nasdaq')===0){
$mk='nasdaq-us';
$nasdaq++;
}elseif(strpos($hay, 'nyse')===0 || strpos($hay, 'new york')===0){
$mk='nyse-us';
$nyse++;
}elseif(strpos($hay, 'cboe')===0){
$mk='cboe-us';
$cboe++;
}else{
$mk='market-us';
$others++;
}
$sc=str_replace(array(' '), array('-'), strtolower($s["quote"]["sector"])); // slug sector
$enc=UpdateStocks::getEnc($symb,$symb,$symb,self::START_POINT_URL_ENCRYPTION_APPEND, self::LENGTH_URL_ENCRYPTION_APPEND); // simple 4 length encryption output: e.g., 159a
$dir=__DIR__ . self::DIR_FRONT_SYMBOLS_MD_FILES; // symbols front directory
if(!is_dir($dir)){mkdir($dir, 0755,true);} // creates price targets directory if not exist
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
// new md filename
$newName=$dir . self::SLASH . $lurl . self::EXTENSION_MD;
// Replace multiple dashes with single dash: "aa-alcoa-basic-materials-nyse-us-159a"
$newName = preg_replace('/-{2,}/', '-', $newName);
// if file not exist: generate file
if($previousNames==null){
$fh=fopen($newName, 'wb');
fwrite($fh, '');
fclose($fh);
}else{
// if file not exist:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
}
// This method is not review required now.
$mdFileContent=UpdateStocks::getBaseHTML($s,$l,$z); // gets HTML
if(file_exists($newName)){
if(is_writable($newName)){
file_put_contents($newName,$mdFileContent);
echo $symb. " 💚 " . self::NEW_LINE;
}else{
echo $symb . " symbol file in front directory is not writable in " . __METHOD__ . " 💔" . self::NEW_LINE;
}
}else{
echo $symb . " file not found in " . __METHOD__ . " 💛" . self::NEW_LINE;
}
}
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
var_dump($hay)
string(20) "nasdaq global"
string(23) "new york stock exchange"
string(20) "nasdaq global market"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "nyse arca"
string(23) "new york stock exchange"
string(23) "nyse"
string(20) "nasdaq"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "cboe"
string(23) "new york stock exchange"
string(20) "nasdaq global select"
string(20) "nasdaq global select"
string(23) "new york stock exchange"
string(23) "new york stock exchange"
...
>5% Probability of var_dump($lurl)
For strtolower($symb)===strtolower($cn)
string(27) "aac-healthcare-nyse-us-e92a"
string(35) "aaon-basic-materials-nasdaq-us-238e"
string(28) "abb-industrials-nyse-us-a407"
string(38) "acnb-financial-services-nasdaq-us-19fa"
<95% Probability of var_dump($lurl)
For not strtolower($symb)===strtolower($cn)
string(50) "aadr-advisorshares-dorsey-wright-adr--nyse-us-d842"
string(39) "aal-airlines-industrials-nasdaq-us-29eb"
string(68) "aamc-altisource-asset-management-com-financial-services-nyse-us-b46a"
string(47) "aame-atlantic-financial-services-nasdaq-us-8944"
string(35) "aan-aarons-industrials-nyse-us-d00e"
string(54) "aaoi-applied-optoelectronics-technology-nasdaq-us-1dee"
string(56) "aap-advance-auto-parts-wi-consumer-cyclical-nyse-us-1f60"
string(36) "aapl-apple-technology-nasdaq-us-8f4c"
string(35) "aat-assets-real-estate-nyse-us-3598"
string(49) "aau-almaden-minerals-basic-materials-nyse-us-1c57"
string(51) "aaww-atlas-air-worldwide-industrials-nasdaq-us-69f3"
string(59) "aaxj-ishares-msci-all-country-asia-ex-japan--nasdaq-us-c6c4"
string(47) "aaxn-axon-enterprise-industrials-nasdaq-us-0eef"
string(58) "ab-alliancebernstein-units-financial-services-nyse-us-deb1"
Would you be so kind and help me to modify it with a faster/simpler script?
performance beginner php strings file-system
performance beginner php strings file-system
edited 26 mins ago
Jamal♦
30.4k11121227
30.4k11121227
asked 10 hours ago
EmmaEmma
177112
177112
1
$begingroup$
You could dosubstr($hey, 0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what$hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
$endgroup$
– ArtisticPhoenix
9 hours ago
add a comment |
1
$begingroup$
You could dosubstr($hey, 0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what$hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
1
$begingroup$
You could do
substr($hey, 0,2)
once and then check the first two letters instead of multiple strpos
. Maybe, but I have no idea what $hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.$endgroup$
– ArtisticPhoenix
9 hours ago
$begingroup$
You could do
substr($hey, 0,2)
once and then check the first two letters instead of multiple strpos
. Maybe, but I have no idea what $hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.$endgroup$
– ArtisticPhoenix
9 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
This is what I meant in the comments
You could do
substr($hey,0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what $hey looks like :) - but than you couldswitch
on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
switch(substr($hay,0,2)){
case 'na': //nasdaq
$mk='nasdaq-us';
$nasdaq++
break;
case 'ny': //nyse
case 'ne': //new york
$mk='nyse-us';
$nyse++;
break;
case 'cb': //cboe
$mk='cboe-us';
$cboe++;
break;
default:
$mk='market-us';
$others++;
break;
}
This way your doing 1 function call instead of up to 4.
It looks like your calling strtolower
more than 3 times on $cn
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // s
//...
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//...
if(strtolower($symb)===strtolower($cn)){
//------------------
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
if(strtolower($symb)===strtolower($cn)){
And so forth.
There may be other duplicate calls like this.
Your sprintf
seem point less.
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//you could just do this for example
$p1=self::SLASH.$symb.'-';
This whole chunk is suspect:
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
For example the only difference is this:
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
//and
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
$lurl=$symb . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
So if you could change the last argument and prepend $symb
, you could maybe eliminate this condition. I have to think about it a bit... lol. But you see what I mean it could be more DRY (Don't repeat yourself). I don't know enough about the data to really say on this one. I was thinking something like this:
if($symb != $cn){
$p = self::SLASH.$symb.'-';
$lurl='';
}else{
$p = self::SLASH.$cn.'-';
$lurl= $symb;
}
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p));
$lurl .= "$cn-$sc-$mk-$enc";
But I am not sure if I got everything strait, lol. So make sure to test it. Kind of hard just working it out in my head. Still need a condition but it's a lot shorter and easier to read.
For this one:
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
You can use preg_grep
. For example:
public static function searchFilenames($array,$re){
return preg_grep('/'.preg_quote($re,'/').'/i',$array);
}
//or array_filter
public static function searchFilenames($array,$re){
return array_filter($array,function($item)use($re){ return strpos($re)!==false;});
}
Your just finding if $re
is contained within each element of $array
. preg_grep — Return array entries that match the pattern
. It's also case insensitive with the i
flag. In any case I never use array_push
as $arr=$str
is much faster. It's even better if you can just modify the array, as this is a function it's like a copy anyway as it's not passed by reference.
One thing I find useful is to take and add some example data values in to the code in comments. Then you can visualize what tranforms your doing and if your repeating yourself.
One last thing this one scares me a bit:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Here your checking that $k
or the array key is 0
, it's very easy to reset array keys when sorting or filtering. So be careful with that, I would think this to be a safer option.
foreach($previousNames as $k=>$previousName){
if($previousName!=$newName){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Not sure if that was a mistake, or maybe I just don't understand that part? It hard without being able to test what the value is. But it warranted mention, once the stuff is deleted its deleted.
Hope it helps you, most of these are minor things, really.
$endgroup$
$begingroup$
Thanks so much! I just updated the question.$hay
is not exactly the same (for example,nasdaq
,nasdaq global
,nasdaq global select
)
$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right aboutsprintf
! I thoughtsprintf
would be faster than.
, which is not: stackoverflow.com/questions/7147305/…
$endgroup$
– Emma
9 hours ago
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215578%2fwriting-a-100kb-html-string-over-an-md-file-number-of-iterations-10k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
This is what I meant in the comments
You could do
substr($hey,0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what $hey looks like :) - but than you couldswitch
on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
switch(substr($hay,0,2)){
case 'na': //nasdaq
$mk='nasdaq-us';
$nasdaq++
break;
case 'ny': //nyse
case 'ne': //new york
$mk='nyse-us';
$nyse++;
break;
case 'cb': //cboe
$mk='cboe-us';
$cboe++;
break;
default:
$mk='market-us';
$others++;
break;
}
This way your doing 1 function call instead of up to 4.
It looks like your calling strtolower
more than 3 times on $cn
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // s
//...
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//...
if(strtolower($symb)===strtolower($cn)){
//------------------
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
if(strtolower($symb)===strtolower($cn)){
And so forth.
There may be other duplicate calls like this.
Your sprintf
seem point less.
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//you could just do this for example
$p1=self::SLASH.$symb.'-';
This whole chunk is suspect:
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
For example the only difference is this:
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
//and
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
$lurl=$symb . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
So if you could change the last argument and prepend $symb
, you could maybe eliminate this condition. I have to think about it a bit... lol. But you see what I mean it could be more DRY (Don't repeat yourself). I don't know enough about the data to really say on this one. I was thinking something like this:
if($symb != $cn){
$p = self::SLASH.$symb.'-';
$lurl='';
}else{
$p = self::SLASH.$cn.'-';
$lurl= $symb;
}
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p));
$lurl .= "$cn-$sc-$mk-$enc";
But I am not sure if I got everything strait, lol. So make sure to test it. Kind of hard just working it out in my head. Still need a condition but it's a lot shorter and easier to read.
For this one:
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
You can use preg_grep
. For example:
public static function searchFilenames($array,$re){
return preg_grep('/'.preg_quote($re,'/').'/i',$array);
}
//or array_filter
public static function searchFilenames($array,$re){
return array_filter($array,function($item)use($re){ return strpos($re)!==false;});
}
Your just finding if $re
is contained within each element of $array
. preg_grep — Return array entries that match the pattern
. It's also case insensitive with the i
flag. In any case I never use array_push
as $arr=$str
is much faster. It's even better if you can just modify the array, as this is a function it's like a copy anyway as it's not passed by reference.
One thing I find useful is to take and add some example data values in to the code in comments. Then you can visualize what tranforms your doing and if your repeating yourself.
One last thing this one scares me a bit:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Here your checking that $k
or the array key is 0
, it's very easy to reset array keys when sorting or filtering. So be careful with that, I would think this to be a safer option.
foreach($previousNames as $k=>$previousName){
if($previousName!=$newName){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Not sure if that was a mistake, or maybe I just don't understand that part? It hard without being able to test what the value is. But it warranted mention, once the stuff is deleted its deleted.
Hope it helps you, most of these are minor things, really.
$endgroup$
$begingroup$
Thanks so much! I just updated the question.$hay
is not exactly the same (for example,nasdaq
,nasdaq global
,nasdaq global select
)
$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right aboutsprintf
! I thoughtsprintf
would be faster than.
, which is not: stackoverflow.com/questions/7147305/…
$endgroup$
– Emma
9 hours ago
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
|
show 5 more comments
$begingroup$
This is what I meant in the comments
You could do
substr($hey,0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what $hey looks like :) - but than you couldswitch
on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
switch(substr($hay,0,2)){
case 'na': //nasdaq
$mk='nasdaq-us';
$nasdaq++
break;
case 'ny': //nyse
case 'ne': //new york
$mk='nyse-us';
$nyse++;
break;
case 'cb': //cboe
$mk='cboe-us';
$cboe++;
break;
default:
$mk='market-us';
$others++;
break;
}
This way your doing 1 function call instead of up to 4.
It looks like your calling strtolower
more than 3 times on $cn
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // s
//...
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//...
if(strtolower($symb)===strtolower($cn)){
//------------------
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
if(strtolower($symb)===strtolower($cn)){
And so forth.
There may be other duplicate calls like this.
Your sprintf
seem point less.
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//you could just do this for example
$p1=self::SLASH.$symb.'-';
This whole chunk is suspect:
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
For example the only difference is this:
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
//and
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
$lurl=$symb . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
So if you could change the last argument and prepend $symb
, you could maybe eliminate this condition. I have to think about it a bit... lol. But you see what I mean it could be more DRY (Don't repeat yourself). I don't know enough about the data to really say on this one. I was thinking something like this:
if($symb != $cn){
$p = self::SLASH.$symb.'-';
$lurl='';
}else{
$p = self::SLASH.$cn.'-';
$lurl= $symb;
}
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p));
$lurl .= "$cn-$sc-$mk-$enc";
But I am not sure if I got everything strait, lol. So make sure to test it. Kind of hard just working it out in my head. Still need a condition but it's a lot shorter and easier to read.
For this one:
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
You can use preg_grep
. For example:
public static function searchFilenames($array,$re){
return preg_grep('/'.preg_quote($re,'/').'/i',$array);
}
//or array_filter
public static function searchFilenames($array,$re){
return array_filter($array,function($item)use($re){ return strpos($re)!==false;});
}
Your just finding if $re
is contained within each element of $array
. preg_grep — Return array entries that match the pattern
. It's also case insensitive with the i
flag. In any case I never use array_push
as $arr=$str
is much faster. It's even better if you can just modify the array, as this is a function it's like a copy anyway as it's not passed by reference.
One thing I find useful is to take and add some example data values in to the code in comments. Then you can visualize what tranforms your doing and if your repeating yourself.
One last thing this one scares me a bit:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Here your checking that $k
or the array key is 0
, it's very easy to reset array keys when sorting or filtering. So be careful with that, I would think this to be a safer option.
foreach($previousNames as $k=>$previousName){
if($previousName!=$newName){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Not sure if that was a mistake, or maybe I just don't understand that part? It hard without being able to test what the value is. But it warranted mention, once the stuff is deleted its deleted.
Hope it helps you, most of these are minor things, really.
$endgroup$
$begingroup$
Thanks so much! I just updated the question.$hay
is not exactly the same (for example,nasdaq
,nasdaq global
,nasdaq global select
)
$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right aboutsprintf
! I thoughtsprintf
would be faster than.
, which is not: stackoverflow.com/questions/7147305/…
$endgroup$
– Emma
9 hours ago
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
|
show 5 more comments
$begingroup$
This is what I meant in the comments
You could do
substr($hey,0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what $hey looks like :) - but than you couldswitch
on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
switch(substr($hay,0,2)){
case 'na': //nasdaq
$mk='nasdaq-us';
$nasdaq++
break;
case 'ny': //nyse
case 'ne': //new york
$mk='nyse-us';
$nyse++;
break;
case 'cb': //cboe
$mk='cboe-us';
$cboe++;
break;
default:
$mk='market-us';
$others++;
break;
}
This way your doing 1 function call instead of up to 4.
It looks like your calling strtolower
more than 3 times on $cn
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // s
//...
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//...
if(strtolower($symb)===strtolower($cn)){
//------------------
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
if(strtolower($symb)===strtolower($cn)){
And so forth.
There may be other duplicate calls like this.
Your sprintf
seem point less.
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//you could just do this for example
$p1=self::SLASH.$symb.'-';
This whole chunk is suspect:
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
For example the only difference is this:
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
//and
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
$lurl=$symb . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
So if you could change the last argument and prepend $symb
, you could maybe eliminate this condition. I have to think about it a bit... lol. But you see what I mean it could be more DRY (Don't repeat yourself). I don't know enough about the data to really say on this one. I was thinking something like this:
if($symb != $cn){
$p = self::SLASH.$symb.'-';
$lurl='';
}else{
$p = self::SLASH.$cn.'-';
$lurl= $symb;
}
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p));
$lurl .= "$cn-$sc-$mk-$enc";
But I am not sure if I got everything strait, lol. So make sure to test it. Kind of hard just working it out in my head. Still need a condition but it's a lot shorter and easier to read.
For this one:
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
You can use preg_grep
. For example:
public static function searchFilenames($array,$re){
return preg_grep('/'.preg_quote($re,'/').'/i',$array);
}
//or array_filter
public static function searchFilenames($array,$re){
return array_filter($array,function($item)use($re){ return strpos($re)!==false;});
}
Your just finding if $re
is contained within each element of $array
. preg_grep — Return array entries that match the pattern
. It's also case insensitive with the i
flag. In any case I never use array_push
as $arr=$str
is much faster. It's even better if you can just modify the array, as this is a function it's like a copy anyway as it's not passed by reference.
One thing I find useful is to take and add some example data values in to the code in comments. Then you can visualize what tranforms your doing and if your repeating yourself.
One last thing this one scares me a bit:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Here your checking that $k
or the array key is 0
, it's very easy to reset array keys when sorting or filtering. So be careful with that, I would think this to be a safer option.
foreach($previousNames as $k=>$previousName){
if($previousName!=$newName){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Not sure if that was a mistake, or maybe I just don't understand that part? It hard without being able to test what the value is. But it warranted mention, once the stuff is deleted its deleted.
Hope it helps you, most of these are minor things, really.
$endgroup$
This is what I meant in the comments
You could do
substr($hey,0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what $hey looks like :) - but than you couldswitch
on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.
switch(substr($hay,0,2)){
case 'na': //nasdaq
$mk='nasdaq-us';
$nasdaq++
break;
case 'ny': //nyse
case 'ne': //new york
$mk='nyse-us';
$nyse++;
break;
case 'cb': //cboe
$mk='cboe-us';
$cboe++;
break;
default:
$mk='market-us';
$others++;
break;
}
This way your doing 1 function call instead of up to 4.
It looks like your calling strtolower
more than 3 times on $cn
$cn=strtolower(UpdateStocks::slugCompany($s["quote"]["companyName"])); // s
//...
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//...
if(strtolower($symb)===strtolower($cn)){
//------------------
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
if(strtolower($symb)===strtolower($cn)){
And so forth.
There may be other duplicate calls like this.
Your sprintf
seem point less.
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
//you could just do this for example
$p1=self::SLASH.$symb.'-';
This whole chunk is suspect:
// symbol in url
$p1=sprintf('%s%s%s',self::SLASH,strtolower($symb),'-');
// company in url
$p2=sprintf('%s%s%s',self::SLASH,strtolower($cn),'-');
// duplication risk
if(strtolower($symb)===strtolower($cn)){
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
}else{
// duplicated name from one symbol
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
$lurl=strtolower($symb) . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
}
For example the only difference is this:
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p2));
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p1));
//and
$lurl=$cn . '-' . $sc . '-' . $mk . '-' . $enc;
$lurl=$symb . '-' . $cn . '-' . $sc . '-' . $mk . '-' . $enc;
So if you could change the last argument and prepend $symb
, you could maybe eliminate this condition. I have to think about it a bit... lol. But you see what I mean it could be more DRY (Don't repeat yourself). I don't know enough about the data to really say on this one. I was thinking something like this:
if($symb != $cn){
$p = self::SLASH.$symb.'-';
$lurl='';
}else{
$p = self::SLASH.$cn.'-';
$lurl= $symb;
}
$previousNames=array_reverse(UpdateStocks::searchFilenames(glob($dir."/*"),$p));
$lurl .= "$cn-$sc-$mk-$enc";
But I am not sure if I got everything strait, lol. So make sure to test it. Kind of hard just working it out in my head. Still need a condition but it's a lot shorter and easier to read.
For this one:
searchFilenames
/**
*
* @return an array with values of paths of all front md files stored
*/
public static function searchFilenames($array,$re){
$arr= array();
foreach($array as $k=>$str){
$pos=strpos($str, $re);
if($pos!==false){
array_push($arr, $str);
}
}
return $arr;
}
You can use preg_grep
. For example:
public static function searchFilenames($array,$re){
return preg_grep('/'.preg_quote($re,'/').'/i',$array);
}
//or array_filter
public static function searchFilenames($array,$re){
return array_filter($array,function($item)use($re){ return strpos($re)!==false;});
}
Your just finding if $re
is contained within each element of $array
. preg_grep — Return array entries that match the pattern
. It's also case insensitive with the i
flag. In any case I never use array_push
as $arr=$str
is much faster. It's even better if you can just modify the array, as this is a function it's like a copy anyway as it's not passed by reference.
One thing I find useful is to take and add some example data values in to the code in comments. Then you can visualize what tranforms your doing and if your repeating yourself.
One last thing this one scares me a bit:
foreach($previousNames as $k=>$previousName){
if($k==0){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Here your checking that $k
or the array key is 0
, it's very easy to reset array keys when sorting or filtering. So be careful with that, I would think this to be a safer option.
foreach($previousNames as $k=>$previousName){
if($previousName!=$newName){
// safety: if previous filename not exactly equal to new filename
rename($previousName, $newName);
}else{
// in case multiple files found: unlink
unlink($previousName);
}
}
Not sure if that was a mistake, or maybe I just don't understand that part? It hard without being able to test what the value is. But it warranted mention, once the stuff is deleted its deleted.
Hope it helps you, most of these are minor things, really.
edited 8 hours ago
answered 9 hours ago
ArtisticPhoenixArtisticPhoenix
25616
25616
$begingroup$
Thanks so much! I just updated the question.$hay
is not exactly the same (for example,nasdaq
,nasdaq global
,nasdaq global select
)
$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right aboutsprintf
! I thoughtsprintf
would be faster than.
, which is not: stackoverflow.com/questions/7147305/…
$endgroup$
– Emma
9 hours ago
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
|
show 5 more comments
$begingroup$
Thanks so much! I just updated the question.$hay
is not exactly the same (for example,nasdaq
,nasdaq global
,nasdaq global select
)
$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right aboutsprintf
! I thoughtsprintf
would be faster than.
, which is not: stackoverflow.com/questions/7147305/…
$endgroup$
– Emma
9 hours ago
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
$begingroup$
Thanks so much! I just updated the question.
$hay
is not exactly the same (for example, nasdaq
, nasdaq global
, nasdaq global select
)$endgroup$
– Emma
9 hours ago
$begingroup$
Thanks so much! I just updated the question.
$hay
is not exactly the same (for example, nasdaq
, nasdaq global
, nasdaq global select
)$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right about
sprintf
! I thought sprintf
would be faster than .
, which is not: stackoverflow.com/questions/7147305/…$endgroup$
– Emma
9 hours ago
$begingroup$
ArtisticPhoenix, You are also right about
sprintf
! I thought sprintf
would be faster than .
, which is not: stackoverflow.com/questions/7147305/…$endgroup$
– Emma
9 hours ago
1
1
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
$begingroup$
Well I usually go over my own code at least three times, once to make it work, once to make it readable and clean up, and once for performance and security. It's like a process like writing a paper.
$endgroup$
– ArtisticPhoenix
9 hours ago
1
1
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
$begingroup$
Yea it's mostly small things, which are easy to get in there when your trying to get it to work. It's got to work first, then you can go look at the logic and flow of things. And see where you can reduce the complexity. That usually makes it easier to read, faster, and less error prone.
$endgroup$
– ArtisticPhoenix
8 hours ago
1
1
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
$begingroup$
I type A LOT of code, so I try to type as little as possible, lol.
$endgroup$
– ArtisticPhoenix
8 hours ago
|
show 5 more comments
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215578%2fwriting-a-100kb-html-string-over-an-md-file-number-of-iterations-10k%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
You could do
substr($hey, 0,2)
once and then check the first two letters instead of multiplestrpos
. Maybe, but I have no idea what$hey
looks like :) - but than you could switch on that and get rid of the multiple function calls. I don't think it will be much faster, but with enough iterations, who knows, and it may look a bit cleaner.$endgroup$
– ArtisticPhoenix
9 hours ago