How best is Ruby's string performance?

Source: Internet
Author: User
1. First read the object_id of the string.

 
Str1 = "anleb" str2 = "anleb" puts str1.object _ idputs str2.object _ id

Output:
22952500
22952490

Different IDs indicate that objects have the same values, but they are different. What is different, that is, pointers are different.
Str1 pointer ----> Object
Str2 pointer ----> Object

ASIDE: Unlike C # and Java, Ruby does not have string objects with the resident technology, but fixnum, false, true, or not. I will show you this experiment.

 
S1 = falses2 = falses3 = trues4 = trues5 = 1s6 = 1 puts s1.object _ idputs s2.object _ idputs s3.object _ idputs s4.object _ idputs s5.object _ idputs s6.object _ id

Output:
0
0
2
2
3
3
The resident technology also exists in Ruby, but unlike other languages, it uses the "symbol" technology to implement it. To put it simply, this article is not for symbols:

 
N1 =: "ID" N2 =: IDP n1.object _ IDP n2.object _ id

Output:
30498
30498

Conclusion: strings appear for performance multiple times. They can be expressed by symbols as much as possible.

2. Check the addition of strings.

Str1 = "anleb" puts str1puts str1.object _ ID #22952460str1 = str1 + "boy" puts str1puts str1.object _ ID #22952430

This indicates that the string addition generates a new object.

 
Str1 = "anleb" puts str1puts str1.object _ ID #22952460str1 = str1 <"boy" puts str1puts str1.object _ ID #22952460

Indicates that the usage of <will not create new objects, but also reduces the memory overhead.

Remark: + = <Concat performance test

Require 'benchmark' n1 = "ABC" N2 = "ABC" N3 = "ABC" benchmark. BM do | BM. report ("<") Do 10000. times {N1 <"ABC"} end BM. report ("+ =") Do 10000. times {N2 + = "ABC"} end BM. report ("Concat") Do 10000. times {n3.concat ("ABC")} end output: User System Total real <0.000000 0.000000 0.000000 (0.000000) + = 0.187000 0.063000 0.250000 (0.266000) Concat 0.016000 0.000000 0.016000 (0.015000)

 
Conclusion: Use <to add strings as much as possible to reduce overhead.

3 "! "
We all know the method is followed! It is a dangerous operation, but why is it dangerous?

 
Str1 = "anleb" puts str1puts str1.object _ idstr2 = str1.capitalizeputs str1puts str2puts str2.object _ idstr3 = str1.capitalize! Puts str1puts str3puts str3.object _ id

Output:
Anleb
22951930
Anleb
Anleb
22951910
Anleb
Anleb
22951930

First look at the ID,! Returns a new string! Is to return the modified original string itself.

4. Finally, copy strings.

 
Str1 = "anleb" str2 = str1p str1.object _ IDP str1.object _ idstr3 = str1.clonep str3.object _ idstr4 = str1.dupp str4.object _ idstr5 = string. New (str1) P str5.object _ id

Output:
22951960
22951960
22951930
22951910
22951890

We all know that str1 and 2 are equivalent. They refer to replication. Here we don't talk about clone, dup, String. New, whether it is shortest replication or deep replication.

The performance of a string depends on the memory overhead of the operating string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.