We are creating UUIDs with 3 different languages, which is faster?

Creating UUID is an effortless task with modern languages, all of them have libraries to develop them straightforwardly, but do they differ…

We are creating UUIDs with 3 different languages, which is faster?
Photo by Karsten Winegeart on Unsplash

Creating UUID is an effortless task with modern languages, all of them have libraries to develop them straightforwardly, but do they differ in terms of speed? how do they compare? In this article, we will see how Bash, Python, and Go compare in terms of speed to generate 100000 UUIDs.

Why I selected Bash, Python, and Go

  • Bash is very good for quick and dirty scripts that you might use once, is always available and many people know how to use it apart from developers (sys admins, devops)
  • Python is used by almost everyone!
  • Go is a very fast language that has gained a lot of popularity in the last few years!

Bash

I choose the simplest way possible, to generate UUID using the uuidgen tool, on my M2 MacBook took 3m26s sounds like quite lot!

for i in {1..100000}; do uuidgen > /dev/null; done

Python

Python has built-in libraries for dealing with UUIDs, on my machine was lightning fast took 0,21sec

#!/usr/bin/env python3 
import uuid 
 
# Generate and print multiple UUIDs 
for _ in range(100000): 
    print(uuid.uuid4())

Go

Using Go needs to initiate our project and download a library for UUID.

Initiate the project

mkdir uuid_gen 
cd uuid_gen 
go mod init uuid_gen

Download the UUID library

go get github.com/google/uuid

Creating, compiling, and running the code

Save the following as main.go

package main 
 
import ( 
    "fmt" 
    "github.com/google/uuid" 
) 
 
func main() { 
    for i := 0; i < 100000; i++ { 
        id := uuid.New() 
        fmt.Println(id.String()) 
    } 
}

Now compile the code

go build main.go

Executing the code on my machine tool 0.08Sec

./main

Results

  • Bash was the slowest, it took 3m26s
  • Python is the second slowest but much faster than Bash, it needed 0,21s
  • Go was the fastest it needed 0,08s

Conclusion

Bash

  • Use it in non-time-critical tasks
  • Use it when Python or Go is not available

Python

  • Good performance
  • Python interpreter probably will be available everywhere
  • If you still need more speed you might want to use Go

Go

  • The fastest
  • The compiler might not be available and need to be installed
  • Requires to download a library