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.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 = 50000;
  16. const bool multiThread = false;
  17. const int threadCount = 50;
  18. const int method = 1;
  19. void RunMultiThread() {
  20. //int64_t start = GetCurrentMicroTime();
  21. for (int i = 0; i < GenIdCount / threadCount; i++) {
  22. int64_t id = NextId();
  23. printf("ID: %D\n", id);
  24. }
  25. int64_t end = GetCurrentMicroTime();
  26. //printf("%s,total:%d μs\n", method == 1 ? "1" : "2", (end - start));
  27. }
  28. void RunSingle() {
  29. int64_t start = GetCurrentMicroTime();
  30. for (int i = 0; i < GenIdCount; i++) {
  31. int64_t id = NextId();
  32. // printf("ID: %ld\n", id);
  33. }
  34. int64_t end = GetCurrentMicroTime();
  35. printf("%s, total: %d us\n", method == 1 ? "1" : "2", (end - start));
  36. }
  37. int main() {
  38. IdGeneratorOptions options = BuildIdGenOptions(1);
  39. options.Method = method;
  40. options.WorkerId = 1;
  41. options.SeqBitLength = 6;
  42. SetIdGenerator(options);
  43. pthread_t tid[threadCount];
  44. while (1) {
  45. if (multiThread) {
  46. for (int i = 0; i < threadCount; i++) {
  47. if (pthread_create(&tid[i], NULL, (void *) RunMultiThread, NULL) != 0) {
  48. printf("thread creation failed\n");
  49. exit(1);
  50. }
  51. }
  52. } else {
  53. RunSingle();
  54. }
  55. sleep(1);
  56. }
  57. }