{"id":21194433,"url":"https://github.com/cspray/precision-stopwatch","last_synced_at":"2025-10-14T21:03:41.174Z","repository":{"id":164916898,"uuid":"637481312","full_name":"cspray/precision-stopwatch","owner":"cspray","description":"A library to precisely time PHP scripts, down to the nanosecond, using hrtime()","archived":false,"fork":false,"pushed_at":"2023-05-13T18:37:28.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-11T00:29:38.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cspray.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-05-07T17:27:17.000Z","updated_at":"2023-05-07T17:27:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"49e161d9-544f-41d0-ba87-6ef4b0d02dfb","html_url":"https://github.com/cspray/precision-stopwatch","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/cspray/precision-stopwatch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fprecision-stopwatch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fprecision-stopwatch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fprecision-stopwatch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fprecision-stopwatch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/precision-stopwatch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fprecision-stopwatch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279011598,"owners_count":26084964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T19:22:10.709Z","updated_at":"2025-10-14T21:03:41.168Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Precision Stopwatch\n \nPrecisely time PHP scripts and code, down to the nanosecond, by utilizing the [hrtime](https://php.net/hrtime) function. \n\n## Installing\n\n[Composer](https://getcomposer.org) is the only supported method for installing this library.\n\n```shell\ncomposer require cspray/precision-stopwatch\n```\n\n## Usage Guide\n\nUsing the provided functionality involves the following steps:\n\n1. Create a new instance of `Cspray\\PrecisionStopwatch\\Stopwatch`\n2. Call `Stopwatch::start()`\n3. Do the thing that you're timing!\n4. Call `Stopwatch::mark()` (optional, see [_Marking Time_](#marking-time))\n5. Call `Stopwatch::stop()`\n6. Retrieve information about how long the Stopwatch ran with `Cspray\\PrecisionStopwatch\\Metrics`\n\nThe code examples below can be executed by cloning this repo and running the scripts available in `./examples`.\n\n### Basic Usage\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse Cspray\\PrecisionStopwatch\\Stopwatch;\n\nrequire_once __DIR__ . '/vendor/autoload.php';\n\n$stopwatch = new Stopwatch();\n\n$stopwatch-\u003estart();\n\nsleep(3);\n\n$metrics = $stopwatch-\u003estop();\n\necho 'Duration (ns): ', $metrics-\u003egetTotalDuration()-\u003etimeTakenInNanoseconds(), PHP_EOL;\necho 'Duration (ms): ', $metrics-\u003egetTotalDuration()-\u003etimeTakenInMilliseconds(), PHP_EOL;\n```\n\nIf you execute this example you should see output similar to the following:\n\n```text\n% \u003e php ./examples/usage-without-marks.php\nTotal time taken (ns): 1000584755\nTotal time taken (ms): 1000.584755\n```\n\n### Marking Time\n\nMarking time allows you to retrieve the duration that a Stopwatch has ran to a certain point, while allowing the Stopwatch to continue running. Calling `Stopwatch::mark()` will return a `Cspray\\PrecisionStopwatch\\Marker` instance. In addition to retrieving the duration up to a certain point, available on the `Marker` instance, you can retrieve the duration between markers with the `Metrics` returned from `Stopwatch::stop()`.\n\n```php\n\u003c?php declare(strict_types=1);\n\nuse Cspray\\PrecisionStopwatch\\Stopwatch;\n\nrequire_once dirname(__DIR__) . '/vendor/autoload.php';\n\n// .75 seconds\n$sleepTime = 750_000;\n\n$stopwatch = new Stopwatch();\n\n$stopwatch-\u003estart();\n\nusleep($sleepTime);\n\n$mark1 = $stopwatch-\u003emark();\n\nusleep($sleepTime);\n\n$mark2 = $stopwatch-\u003emark();\n\nusleep($sleepTime);\n\n$mark3 = $stopwatch-\u003emark();\n\nusleep($sleepTime);\n\n$metrics = $stopwatch-\u003estop();\n\necho 'Total time taken (ns): ', $metrics-\u003egetTotalDuration()-\u003etimeTakenInNanoseconds(), PHP_EOL;\necho 'Total time taken (ms): ', $metrics-\u003egetTotalDuration()-\u003etimeTakenInMilliseconds(), PHP_EOL;\n\necho PHP_EOL;\n\n$between1And3 = $metrics-\u003egetDurationBetweenMarkers($mark1, $mark3);\necho 'Time take between 1st and 3rd mark (ns): ', $between1And3-\u003etimeTakenInNanoseconds(), PHP_EOL;\necho 'Time take between 1st and 3rd mark (ms): ', $between1And3-\u003etimeTakenInMilliseconds(), PHP_EOL;\n\necho PHP_EOL;\n\necho 'Time taken up to 3rd mark (ns): ', $mark3-\u003egetDuration()-\u003etimeTakenInNanoseconds(), PHP_EOL;\necho 'Time taken up to 3rd mark (ms): ' , $mark3-\u003egetDuration()-\u003etimeTakenInMilliseconds(), PHP_EOL;\n```\n\nIf you execute this example you should see output similar to the following:\n\n```text\n% \u003e php ./examples/usage-without-marks.php\nTotal time taken (ns): 3000608258\nTotal time taken (ms): 3000.608258\n\nTime take between 1st and 3rd mark (ns): 1500407710\nTime take between 1st and 3rd mark (ms): 1500.40771\n\nTime taken up to 3rd mark (ns): 2250497069\nTime taken up to 3rd mark (ms): 2250.497069\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fprecision-stopwatch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fprecision-stopwatch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fprecision-stopwatch/lists"}