Do you know why rails like using symbol instead of string as key for hash?
the answer is,
when comparing symbol, it find the symbol object_id, then find the symbol,
Because there is only one copy of the same symbol in the memory in ruby.
when comparing string, it has to compare the characters in the string one by one,
Each string has its own copy, even if they are the same string.
you know this is much expensive than comparing symbol!!
1. they can be converted between each other:
"hello".to_sym => :hello
"hello".intern => :hello
:hello.to_s => "hello"
"hello world" == :"hello world"
2. symbol is a constant, can't be changed.
string is a variable, can be changed.
puts "hello" << " world"
=> "hello world"
puts :hello << :" world"
=> error, undefined method << for symbol.
sometimes, your program will rely on a string as a flag, and this flag should never change, if you use a common, string, it may be changed by mistake, for example:
status = "peace"
buggy_logger = status
print buggy_logger << "\n"
in the above code, the string status is changed by mistake.
to avoid this mistake, there are some methods:
a. use clone:
status = "peace"
buggy_logger = status.clone
by this way, if you change buggy_logger, status will not be affected.
b. use freeze:
status = "peace"
status.freeze
then it will report error if some code want to change the content of status.
c. use symbol.
3. overhead, the resource by symbol is much less.
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
# => 3102960
# => 3098410
# => 3093860
# => 3089330
# => 3084800
you can see the object.id is different for the same string.
but for symbol:
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
# => 239518
# => 239518
# => 239518
# => 239518
# => 239518
They are the same.
4. there is a variable Symbol that hold all existing symbol:
puts Symbol.all_symbols.inspect
5. how to test the performance of a block of code:
require 'benchmark'
str_time = Benchmark.measure do
10000000.times do
"test"
end
end.total
symbol_time = Benchmark.measure do
10000000.times do
:test
end
end.total
puts "String: " + str.to_s
puts "Symbol: " + sym.to_s
the answer is,
when comparing symbol, it find the symbol object_id, then find the symbol,
Because there is only one copy of the same symbol in the memory in ruby.
when comparing string, it has to compare the characters in the string one by one,
Each string has its own copy, even if they are the same string.
you know this is much expensive than comparing symbol!!
1. they can be converted between each other:
"hello".to_sym => :hello
"hello".intern => :hello
:hello.to_s => "hello"
"hello world" == :"hello world"
2. symbol is a constant, can't be changed.
string is a variable, can be changed.
puts "hello" << " world"
=> "hello world"
puts :hello << :" world"
=> error, undefined method << for symbol.
sometimes, your program will rely on a string as a flag, and this flag should never change, if you use a common, string, it may be changed by mistake, for example:
status = "peace"
buggy_logger = status
print buggy_logger << "\n"
in the above code, the string status is changed by mistake.
to avoid this mistake, there are some methods:
a. use clone:
status = "peace"
buggy_logger = status.clone
by this way, if you change buggy_logger, status will not be affected.
b. use freeze:
status = "peace"
status.freeze
then it will report error if some code want to change the content of status.
c. use symbol.
3. overhead, the resource by symbol is much less.
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
# => 3102960
# => 3098410
# => 3093860
# => 3089330
# => 3084800
you can see the object.id is different for the same string.
but for symbol:
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
# => 239518
# => 239518
# => 239518
# => 239518
# => 239518
They are the same.
4. there is a variable Symbol that hold all existing symbol:
puts Symbol.all_symbols.inspect
5. how to test the performance of a block of code:
require 'benchmark'
str_time = Benchmark.measure do
10000000.times do
"test"
end
end.total
symbol_time = Benchmark.measure do
10000000.times do
:test
end
end.total
puts "String: " + str.to_s
puts "Symbol: " + sym.to_s
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 735the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8031. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8751. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8341.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 6991. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8541. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6471. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10621. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 685in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 728in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 601again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6611. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 679whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 6801. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 668There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 602start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 610a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 675in integration test, you can te ...
相关推荐
Difference between HBase and RDBMS
state 50 differences between marketing and sales
What's the difference between TAF and FCF
java and C++ difference
Summary: <br/>Symptom: This is an explanation of differences between TYPE and LIKE additions <br/>Additional key words: Data types, data objects, ABAP type concept, Dictionary ...
1、Explain the difference between internal and external fragmentation. 2、Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600KB (in order), how would each of the first-fit, best-...
Data collection at ultra high-frequency on ... Overall the difference in the coefficients is bigger between the dirty series and the clean series than among series filtered with different algorithms.
FX2 and FX2LP 系列的区别 cy7c68013A开发套餐
易趣网买家和卖家解决在线争端的不同方式,董宝田,李玮,有关在线争端解决的研究开始于二十世纪末期,现如今,随着电子商务爆发式的发展,怎样解决在线争端变得尤为重要。作为全球最大的网上
Difference Between VMWare ESX and VMWare ESXi
What's the Difference between Pneumatic, Hydraulic, and Electrical Actuators.docx
N-磷酰化多肽的质子加合峰与钠离子加合峰的多级质谱裂解规律的差异性研究,刘艳,陈培燕,利用三种不同的磷酰化试剂,分别对胸腺肽的肌动蛋白结合域进行N-磷酰化修饰,并通过ESI-MS/MS对其进行序列测定。...
The difference between the smaller, downloadable .Net Framework and the full desktop version is one of the major disparities between the two platforms. In addition, the fact that Silverlight is ...
IE和Firefox下开发时会遇到的问题
《通信网基本概念及其主体结构》一书 (第二版)课后练习答案。主要是问答和简述以及设计题的解答。
sva classical q&a Qi1)What is callback ? (Qi2)What is factory pattern ?...(Qi3)Explain the difference between data types logic and reg and wire . (Qi4)What is the need of clocking blocks ?
Time Dependent Problems and Difference Methods addresses these various industrial considerations in a pragmatic and detailed manner, giving special attention to time dependent problems in its coverage...
the difference of Invoke and BeginInvoke
The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, ...class and function templates, the difference between lvalue and rvalue references, and so on.
等效采样示波器与实时采样示波器区别