Skip to content

Commit

Permalink
Refactor CpuCoreCounter (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 4, 2022
1 parent 6d3e2af commit 5392f6a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ final class CpuCoreCounter
*/
public function getCount(): int
{
if (isset($this->count)) {
return $this->count;
// Memoize result
if (!isset($this->count)) {
$this->count = self::findCount();
}

return $this->count;
}

/**
* @return positive-int
*/
private static function findCount(): int
{
if (!function_exists('proc_open')) {
return $this->count = 1;
return 1;
}

// from brianium/paratest
Expand All @@ -49,7 +58,7 @@ public function getCount(): int
if (false !== $cpuinfo) {
preg_match_all('/^processor/m', $cpuinfo, $matches);

return $this->count = count($matches[0]);
return count($matches[0]);
}
}

Expand All @@ -62,7 +71,7 @@ public function getCount(): int
$cores = (int) fgets($process);
pclose($process);

return $this->count = $cores;
return $cores;
}
}

Expand All @@ -73,9 +82,9 @@ public function getCount(): int
$cores = (int) fgets($process);
pclose($process);

return $this->count = $cores;
return $cores;
}

return $this->count = 2;
return 2;
}
}

0 comments on commit 5392f6a

Please sign in to comment.