5 ms·
I get ~20% faster times with the first style than with f-strings for this specific use, which is when I discovered it. def test_with_cstr(epoch: float) ->
by sgarland 2y ago
I get ~20% faster times with the first style than with f-strings for this specific use, which is when I discovered it.
def test_with_cstr(epoch: float) -> str:
return "%04d-%02d-%02d %02d:%02d:%02d" % (time.gmtime(epoch)[:6])
def test_with_fstr(dt: datetime) -> str:
return f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d} {dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}"
EDIT: bad example, there’s other function overhead here as well.