GithubHelp home page GithubHelp logo

allnightcoder / uniqueidgenerator-dotnet5 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mschuler/uniqueidgenerator

0.0 0.0 0.0 28 KB

C# implementation of Twitter's Snowflake

License: MIT License

C# 100.00%

uniqueidgenerator-dotnet5's Introduction

Unique Id Generator

C# implementation of Twitter's Snowflake. A Generator can create sequentially unique IDs with a length of 8 bytes.
The ID consists of a timestamp (milliseconds since specific start date), a generator id and a sequence.

This is the default configuration:

  • 41 bits for timestamp in milliseconds (can vary from 40 to 45)
  • 10 bits for generator id (can vary from 0 to 10)
  • 13 bits for sequence (can vary from 12 to 20)

You can change this default values to gain more performance on a single process (more sequence-bits) or more performance in a cluster (more generator-bits).
With the default configuration you can create 1024 different generators. Each generator can create up to 8192 unique IDs per millisecond for 69 years.

Usage

First, grab the assembly from NuGet. I haven't created a nuget package yet.

// Create a new instance of the generator
var generator = new Generator(machineId, projectStartDate);
    
// get a new id
generator.Next();

Performance measurement

Environment:

  • Intel Core i7-3740QM (2.7GHz)
  • 8GB RAM
  • Microsoft Windows 8.0 x64
  • Default configuration (41/10/13)

Single generator

Duration to generate 10'000'000 ids: 1'847 ms
Number of ids generated in 1 ms: 5'414
Number of ids generated in 1 s: 5'414'185

4 generators

Duration to generate 20'000'000 ids: 2'506 ms
Number of ids generated in 1 ms: 7'980
Number of ids generated in 1 s: 7'980'845

8 generators

Duration to generate 8'000'000 ids: 802 ms
Number of ids generated in 1 ms: 9'975
Number of ids generated in 1 s: 9'975'062

16 generators

Duration to generate 16'000'000 ids: 1'569 ms
Number of ids generated in 1 ms: 10'197
Number of ids generated in 1 s: 10'197'578

32 generators

Duration to generate 16'000'000 ids: 1'657 ms
Number of ids generated in 1 ms: 9'656
Number of ids generated in 1 s: 9'656'004

64 generators

Duration to generate 12'800'000 ids: 1'510 ms
Number of ids generated in 1 ms: 8'476
Number of ids generated in 1 s: 8'476'821

License

Developed by Michael Schuler under the MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.