Skip to content

Instantly share code, notes, and snippets.

@CandyMi
Last active June 7, 2024 08:50
Show Gist options
  • Save CandyMi/9b57e8f9c5e28cef12fb0985a8a636c2 to your computer and use it in GitHub Desktop.
Save CandyMi/9b57e8f9c5e28cef12fb0985a8a636c2 to your computer and use it in GitHub Desktop.
zlib vs lz4 vs snappy

测试描述

本次对比常用库包括:

  • zlib
  • lz4
  • snappy

压缩测试

以下均使用默认默认参数:

算法名称 英文大小 英文压缩比 中文大小 中文压缩比
text 1272 100% 1238 100%
zlib 540 42.45% 870 70.27%
lz4 801 62.97% 1093 88.29%
snappy 788 61.95% 1084 87.56%

耗时测试

循环运行10w次, 多次后取平均值耗时. (数字越小越好)

开启优化

算法名称 英文压缩 英文解压 中文压缩 中文解压
zlib 1.482/Sec 0.395/Sec 5.011/Sec 0.551/Sec
lz4 0.152/Sec 0.033/Sec 0.165/Sec 0.037/Sec
snappy 0.126/Sec 0.090/Sec 0.164/Sec 0.109/Sec
local lz4 = require "lz4"
local zlib = require "zlib"
local snappy = require "snappy"
local zstd = require "zstd"
local brotli = require "brotli"
-- local text = [==[
-- 完善的异步I/O支持
-- 框架已经对网络、文件两种IO进行改造, 使内部的I/O全异步化成为可能.
-- 框架自动化调度
-- 框架会在内部自动调度协程、定时器、I/O, 您也可以选择手动控制它.
-- 丰富的内置库支持
-- 框架内部的提供了完整的内置库, 实现了基础应用全方面开发覆盖.
-- 传输安全与加密
-- 提供近60多种加密/散列/签名等等算法, 并且内部已经支持SSL Server / Client.
-- 数据交换格式支持
-- 您需要 JSON / PROTOBUF / XML / MSGPACK 吗? 那么非常幸运, 它们都提供好了!
-- 内置协议多样化
-- 支持常见的 HTTP[S]/STOMP/SMTP/ Websocket 等协议.
-- 自主重写异步客户端
-- 让我们可以随心所欲的使用. (Redis/MySQL/PGSQL/MSSQL)
-- 合理的进程运行模式
-- 拆分工作进程与管理进程,运行模式更加准确且合理。
-- 资源的高效利用
-- 完美的将所有内存、核心数量都妥善使用。
-- 适用于各种部署场景
-- 无论虚拟化、容器、宿主机,这都是我们的主要战场.
-- 带宽利用与性能优化
-- 传输数据压缩与数据库连接池的利用都不用手动维护
-- 强制化与规范化
-- 框架首先教您如何规范项目, 同样您也可以将其传授给挚友.
-- ]==]
-- local text = [==[
-- Advantage
-- Asynchronous I/O - The Network I/O and File I/O have been transformed, and the internal operations are now fully asynchronous.
-- Rich built-in libraries - Many complete built-in libraries are implemented to complete the development and coverage of all aspects of basic applications.
-- Automated scheduling - The bottom layer will automatically schedule coroutines, timers and I/O, and you can also choose to control it manually.
-- Security and Encryption - Provides nearly 60 kinds of hash/digest/hash/signature algorithms, and internally supports SSL Server/Client.
-- Data Exchange Format - Do you need JSON / PROTOBUF / MSGPACK / BSON ? Great, they are all provided!
-- Data Exchange Format - Do you need JSON / PROTOBUF / MSGPACK / BSON ? Great, they are all provided!
-- Multi-database driver - MySQL / PGSQL / MSSQL / MongoDB are all available, you can also use it as you like.
-- Multi-database driver - MySQL / PGSQL / MSSQL / MongoDB are all available, you can also use it as you like.
-- Enforcement and standardization -The framework forces you to write code, and you can also pass it on to your close friends.
-- Enforcement and standardization -The framework forces you to write code, and you can also pass it on to your close friends.
-- ]==]
local function test_f(f, data, n, v)
local assert = assert
for i = 1, n do
assert(f(data, v))
end
end
local function calc_run(f, ...)
local s = require "sys".time()
f(...)
local e = require "sys".time()
return e - s
end
local n = 100000
local list = {
{ name = 'zlib', compress = zlib.compress, uncompress = zlib.uncompress, data = zlib.compress(text), raw = text },
{ name = 'lz4', compress = lz4.compress, uncompress = lz4.uncompress, data = lz4.compress(text), raw = text, len = #text },
{ name = 'zstd', compress = zstd.compress, uncompress = zstd.uncompress, data = zstd.compress(text), raw = text },
{ name = 'brotli', compress = brotli.compress, uncompress = brotli.uncompress, data = brotli.compress(text), raw = text },
{ name = 'snappy', compress = snappy.compress, uncompress = snappy.uncompress, data = snappy.compress(text), raw = text },
}
print("----")
for _, obj in ipairs(list) do
print(string.format("%s.compress %d 万次, 耗时: %f 秒", obj.name, n / 10000, calc_run(test_f, obj.compress, text, n)))
end
print("----")
for _, obj in ipairs(list) do
print(string.format("%s.uncompress %d 万次, 耗时: %f 秒", obj.name, n / 10000, calc_run(test_f, obj.uncompress, obj.data, n, obj.len)))
end
print("----")
print('raw length = ', #text)
local r = ""
for _, obj in ipairs(list) do
r = r .. string.format("%s compress length = %s, radiot = %.2f%%\n", obj.name, #obj.data, #obj.data / #text * 100)
end
print(r)
print("----")
print("End.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment