DEV Community

Pranoy Dev for Innovation Incubator

Posted on • Originally published at Medium on

REST API Performance Comparison Python vs Golang

In this post, we’re going to explore building APIs with Python and GO. We’re going to put these two languages to the test and grade their performance based on response time. I’ll be using Postman for API testing. REST APIs in Python is powered by Flask and Mux for Golang

We are going to perform CRUD operations onto a PostgreSQL instance running on localhost using python and go and rate them across each category.

What is CRUD? It stands for Create, Read, Update and Delete, these are the basic operations performed on a database. Read more about it from the below reference.

Create, read, update and delete

First, we have to set up the PostgreSQL instance and create a table onto which we can perform the operations.

here we create a simple table called data with a single text field. We’re going to perform the CRUD processes on this table inside the Postgres database.

The code repo can be found at

devpranoy/CRUD-pygo

The APIs have been built in a way that is similar to the following architecture.

Now let’s Analyse the response times. Let’s fire up Postman and checkout the response times for

CREATE

Python:

Golang:

Winner: Golang , while python was averaging 20 to 30 ms, Golang was doing 15 to 20 ms winning by a margin of 5 -10 ms

READ

Python:

Golang:

Winner: Golang , while python was averaging 15to 20ms, Golang was doing 12 to 15ms winning by a margin of 3 - 5 ms

UPDATE

Python:

Golang:

Winner: Golang, while python was averaging 20 to 25ms, Golang was doing 15 to 20ms winning by a margin of 5 ms

DELETE

Python:

Golang:

Winner: Golang by a small margin of 2 to 3ms when compared to Python

Conclusion

GO is faster than Python, duh! but brownie points to Python for being soo easy to get up and running. But the goal of this experiment was to judge the impact that migrating to Golang would bring from a business perspective. Assume that 1 million API calls are made, Say python takes 20ms per request and Golang takes 15ms per request, Total processing time in Python would take around 5.55 hours and Golang would take 4.16 hours. The difference a few milliseconds make is huge. Since most businesses use AWS for building and deploying APIs, Golang will help save a huge sum of money as AWS charges per time taken for execution.


Top comments (3)

Collapse
 
tbublik profile image
Timur Bublik

Thanks for post. I think that title is a bit misleading. It is rather comparison of two particular frameworks. There are actually faster Python frameworks out there. It would also be good to put the API in a remote box and provide Python and Golang versions used.
Comparison of languages performance in REST API would in my opinion look more like AWS API GW + Lambda.

Collapse
 
timotheejeannin profile image
Timothée Jeannin

Thank you for taking the time to make this benchmark.

I think it would be more appropriate to use a tool like wrk to make a lot of concurrent requests and make sure all CPU cores are processing requests at 100% capacity.

In your tests, both the python code and the golang code are most of the time waiting for the database to respond. The CPU usage might be very low. Maybe the python code makes a small spike at 25% CPU on one core and the golang code make a small spike at 20% CPU on one code.

The time it takes to process one request is not a good indicator of how many requests per second can be made.

Collapse
 
yokotobe profile image
yokotobe

Thanks for benchmakring
Could you provide tested Go and Python version to this community?

ref: speed.python.org/comparison/