AI performance is good

which fastapi or sanic is better for AI performance?

Let me start by saying that benchmarks are not absolute. They are, at best, some sort of guideline; not truth themselves. They are inherently biased and not necessarily indicative of how production applications will behave.

Another caveat is that Sanic and FastAPI are very different. Sanic is both a web server and a framework. FastAPI is an interface layer on top of a framework (Starlette), and is a “bring your own server.” I will not claim to speak for FastAPI, but from my vantage it has more logic rolled into it and is therefore more opionated than Sanic. One of the core principles of Sanic is that it is meant to be a tool for the developer to build how s/he wants, and not to make decisions for you. While not exactly the same, it is similar to the difference between Django and Flask.

With that out of the way, and if you are to believe benchmarks:

Framework Speed (64) Speed (256) Speed (512)
Sanic 31 014.14 34 318.11 35 124.86
FastAPI 18 345.04 21 573.15 22 034.75

According to this, Sanic is ~60% faster



According to this, Sanic is ~20% faster.

In regards to any other work that the application would be doing (AI or otherwise), that would be entirely based upon the libraries you use and the code logic implemented.

One thing to note is that Sanic does provide a an API for pushing tasks off to the background, which is often a boon for performance. I do not know if FastAPI offers something similar. It is (however) a sort of convenience layer on top of loop.create_task.

1 Like