有谁试过 redis 的 string 自增性能吗, 我这怎么是个位数?

查看 65|回复 4
作者:bthulu   
我一直用这个来生成多个表公用的自增 id.
今天闲来无聊测了下, tps=10, 这有点低的离谱啊
测试代码
        ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
        Stopwatch stopwatch = Stopwatch.StartNew();
        var count = 100000;
        for (int i = 0; i
测试结果
用时:8357, tps=11

stopwatch, Redis, tps, count

sdlzqjf   
from chatgpt:
这段代码的问题在于每次循环都会创建一个新的 Redis 连接。在循环中频繁地创建和关闭连接会导致性能下降,并且可能会对 Redis 服务器造成不必要的负担。
为了改进代码,可以将连接创建和关闭的逻辑移动到循环外部,并且在循环内重复使用同一个连接对象。这样可以减少连接的创建和关闭次数,提高性能。
以下是修改后的代码示例:
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
Stopwatch stopwatch = Stopwatch.StartNew();
var count = 100000;
var db = redis.GetDatabase();
for (int i = 0; i < count; i++)
{
var id = (int)db.StringIncrement("PMDCS:id_hello");
}
stopwatch.Stop();
Console.WriteLine("用时:" + stopwatch.ElapsedMilliseconds + ", tps=" + (count / stopwatch.ElapsedMilliseconds));
这样修改后,连接只会在循环外部创建一次,并在循环内重复使用。这样可以提高性能并减少对 Redis 服务器的负担。
wunonglin   
@Livid #1 ChatGPT
lingalonely   
ElapsedMilliseconds 不是毫秒吗,tps 是时间单位是秒呀
sadfQED2   
@sdlzqjf 一本正经的胡说八道
您需要登录后才可以回帖 登录 | 立即注册

返回顶部