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.

main.c 1.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码翻译:amuluowin
  4. * 代码修订:yitter
  5. * 开源地址:https://gitee.com/yitter/idgenerator
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/timeb.h>
  10. #include <pthread.h>
  11. #include <unistd.h>
  12. #include <stdbool.h>
  13. #include "idgen/SnowWorkerM1.h"
  14. #include "idgen/IdGenerator.h"
  15. #include "YitIdHelper.h"
  16. const int GenIdCount = 50000;
  17. const bool multiThread = false;
  18. const int threadCount = 50;
  19. const int method = 1;
  20. void RunMultiThread() {
  21. //int64_t start = GetCurrentMicroTime();
  22. for (int i = 0; i < GenIdCount / threadCount; i++) {
  23. int64_t id = NextId();
  24. printf("ID: %D\n", id);
  25. }
  26. int64_t end = GetCurrentMicroTime();
  27. //printf("%s,total:%d μs\n", method == 1 ? "1" : "2", (end - start));
  28. }
  29. void RunSingle() {
  30. int64_t start = GetCurrentMicroTime();
  31. for (int i = 0; i < GenIdCount; i++) {
  32. int64_t id = NextId();
  33. // printf("ID: %ld\n", id);
  34. }
  35. int64_t end = GetCurrentMicroTime();
  36. printf("%s, total: %d us\n", method == 1 ? "1" : "2", (end - start));
  37. }
  38. int main() {
  39. IdGeneratorOptions options = BuildIdGenOptions(1);
  40. options.Method = method;
  41. options.WorkerId = 1;
  42. options.SeqBitLength = 6;
  43. SetIdGenerator(options);
  44. pthread_t tid[threadCount];
  45. while (1) {
  46. if (multiThread) {
  47. for (int i = 0; i < threadCount; i++) {
  48. if (pthread_create(&tid[i], NULL, (void *) RunMultiThread, NULL) != 0) {
  49. printf("thread creation failed\n");
  50. exit(1);
  51. }
  52. }
  53. } else {
  54. RunSingle();
  55. }
  56. sleep(1);
  57. }
  58. }

雪花算法中非常好用的数字ID生成器