You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

snowdrift.php 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. namespace SnowDrift;
  4. use FFI;
  5. use FFI\CData;
  6. final class SnowFlake
  7. {
  8. private FFI $ffi;
  9. private CData $flake;
  10. private CData $pflake;
  11. public function __construct(array $config = ['Method' => 1, 'BaseTime' => 0, 'WorkerId' => 1, 'WorkerIdBitLength' => 6, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000])
  12. {
  13. $this->ffi = FFI::cdef(file_get_contents($this->getHeaders()), $this->getLibrary());
  14. $this->flake = $this->ffi->new("struct snowflake");
  15. foreach ($config as $name => $val) {
  16. $this->flake->$name = $val;
  17. }
  18. $this->pflake = FFI::addr($this->flake);
  19. $this->ffi->Config($this->pflake);
  20. }
  21. public function getFFI(): FFI
  22. {
  23. return $this->ffi;
  24. }
  25. public function getFlake(): CData
  26. {
  27. return $this->flake;
  28. }
  29. public function getPflake(): CData
  30. {
  31. return $this->pflake;
  32. }
  33. public function nextId(): int
  34. {
  35. return $this->ffi->NextId($this->pflake);
  36. }
  37. public function getHeaders(): string
  38. {
  39. return __DIR__ . '/src/snowflake/snowflake.h';
  40. }
  41. public function getLibrary(): ?string
  42. {
  43. return __DIR__ . '/src/snowflake/libsnow.so';
  44. }
  45. }
  46. $total = 50000;
  47. $snowflake = new SnowFlake(['Method' => 1, 'BaseTime' => 1577808000000, 'WorkerId' => 1, 'WorkerIdBitLength' => 1, 'SeqBitLength' => 10, 'TopOverCostCount' => 2000]);
  48. $ffi = $snowflake->getFFI();
  49. $pflake = $snowflake->getPflake();
  50. // $res = [];
  51. $start = microtime(true);
  52. for ($i = 0; $i < $total; $i++) {
  53. // $res[] = \SnowDrift::NextId();
  54. \SnowDrift::NextId(2);
  55. }
  56. echo sprintf("扩展漂移算法,PHP循环获取:%d,%s ms", $total, ((microtime(true) - $start)) * 1000) . PHP_EOL;
  57. // $res = [];
  58. $start = microtime(true);
  59. foreach (\SnowDrift::NextNumId($total) as $val) {
  60. // $res[] = $val;
  61. }
  62. echo sprintf("扩展漂移算法,C循环获取:%d,%s ms", $total, ((microtime(true) - $start)) * 1000) . PHP_EOL;
  63. // $res = [];
  64. $start = microtime(true);
  65. for ($i = 0; $i < $total; $i++) {
  66. // $res[] = $ffi->NextId($pflake);
  67. $ffi->NextId($pflake);
  68. }
  69. echo sprintf("FFI漂移算法,PHP循环获取:%d,%s ms", $total, ((microtime(true) - $start)) * 1000) . PHP_EOL;
  70. // $res = [];
  71. $start = microtime(true);
  72. $tmp = $ffi->NextNumId($pflake, $total);
  73. for ($i = 0; $i < $total; $i++) {
  74. // $res[] = $tmp[$i];
  75. }
  76. echo sprintf("FFI漂移算法,C循环获取:%d,%s ms", $total, ((microtime(true) - $start)) * 1000) . PHP_EOL;