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