Project

General

Profile

Actions

Bug #20512

closed

Order of magnitude performance differenfce in single character slicing UTF-8 strings before and after length method is executed

Added by giner (Stanislav German-Evtushenko) 6 months ago. Updated 6 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
[ruby-core:118037]

Description

Slicing of a single character of UTF-8 string becomes ~15 times faster after method "length" is executed on the string.

# Single byte symbols
letters = ("a".."z").to_a
length = 100000

str = length.times.map{letters[rand(26)]}.join

# Slow
start = Time.now
length.times{|i| str[i]}
puts Time.now - start  # 0.169156201

str.length  # performance hack

# Fast
start = Time.now
length.times{|i| str[i]}
puts Time.now - start  # 0.009883919


# UTF-8 Symbols
letters = ("а".."я").to_a
length = 10000

str = length.times.map{letters[rand(26)]}.join

# Slow
start = Time.now
length.times{|i| str[i]}
puts Time.now - start  # 0.326204007

str.length  # performance hack

# Fast
start = Time.now
length.times{|i| str[i]}
puts Time.now - start  # 0.016943093
Actions

Also available in: Atom PDF

Like0
Like0Like0