1
0
mirror of synced 2025-12-27 07:27:58 +08:00

commitClang

This commit is contained in:
yitter
2021-03-27 22:00:52 +08:00
parent 5fb6e03936
commit 4233840307
16 changed files with 857 additions and 2 deletions

67
C/source/main.c Normal file
View File

@@ -0,0 +1,67 @@
/*
* 版权属于yitter(yitter@126.com)
* 代码翻译amuluowin
* 代码修订yitter
* 开源地址https://gitee.com/yitter/idgenerator
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>
#include "idgen/SnowWorkerM1.h"
#include "idgen/IdGenerator.h"
#include "YitIdHelper.h"
const int GenIdCount = 50000;
const bool multiThread = false;
const int threadCount = 50;
const int method = 1;
void RunMultiThread() {
//int64_t start = GetCurrentMicroTime();
for (int i = 0; i < GenIdCount / threadCount; i++) {
int64_t id = NextId();
printf("生成ID: %ld\n", id);
}
int64_t end = GetCurrentMicroTime();
//printf("%stotal%d μs\n", method == 1 ? "1" : "2", (end - start));
}
void RunSingle() {
int64_t start = GetCurrentMicroTime();
for (int i = 0; i < GenIdCount; i++) {
int64_t id = NextId();
// printf("生成ID: %ld\n", id);
}
int64_t end = GetCurrentMicroTime();
printf("%stotal%d μs\n", method == 1 ? "1" : "2", (end - start));
}
int main() {
IdGeneratorOptions options = BuildIdGenOptions(1);
options.Method = method;
options.WorkerId = 1;
options.SeqBitLength = 6;
SetIdGenerator(options);
pthread_t tid[threadCount];
while (1) {
if (multiThread) {
for (int i = 0; i < threadCount; i++) {
if (pthread_create(&tid[i], NULL, (void *) RunMultiThread, NULL) != 0) {
printf("thread creation failed\n");
exit(1);
}
}
} else {
RunSingle();
}
sleep(1);
}
}