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 816the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8521. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9101. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8761.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7471. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8931. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6981. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 733in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 768in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 657again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7051. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 732whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7411. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 655start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 643a 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 714in integration test, you can te ...
相关推荐
市场营销与销售是商业活动中两个密切相关的但又有所区别的领域。它们之间的差异可以从多个角度进行阐述: 1. 目标与期限:市场营销关注长期的竞争优势和未来市场趋势,旨在解决问题并创造需求;...
在IT领域,数据库的选择对于系统设计至关重要。本文将深入探讨两种截然不同的数据库类型:HBase和RDBMS(关系型数据库管理系统)之间的差异,以及为什么在某些情况下选择HBase可能更为合适。 HBase,全称为Hadoop ...
java and C++ difference
透明应用故障切换(TAF)和快速连接故障切换(FCF)是两种在高可用性环境中用于处理数据库连接故障的机制。它们都是Oracle数据库提供的功能,以确保在系统出现故障时,应用程序能够继续运行,而不会中断服务。...
### SAP Notes 176337: 关于 TYPE 和 LIKE 的区别 #### 概述 在 SAP 的 ABAP 编程环境中,了解数据类型 (`Datatypes`) 和数据对象 (`Data Objects`) 之间的区别至关重要。这不仅关系到编程时如何正确声明变量,还...
- 零长度IN包与ISO传输:FX2LP允许在无需固件干预的情况下处理零长度的IN包,并改进了ISO传输的数据PID顺序。 - ECC(Error-Correcting Code)生成:在FX2LP中,GPIF(通用可编程接口)数据现在支持ECC生成,增强...
易趣网买家和卖家解决在线争端的不同方式,董宝田,李玮,有关在线争端解决的研究开始于二十世纪末期,现如今,随着电子商务爆发式的发展,怎样解决在线争端变得尤为重要。作为全球最大的网上
N-磷酰化多肽的质子加合峰与钠离子加合峰的多级质谱裂解规律的差异性研究,刘艳,陈培燕,利用三种不同的磷酰化试剂,分别对胸腺肽的肌动蛋白结合域进行N-磷酰化修饰,并通过ESI-MS/MS对其进行序列测定。...
在压缩包"develop in IE and Firefox"中,可能包含的是针对这两种浏览器开发的示例代码、测试用例或解决兼容性问题的策略,可以帮助开发者更好地理解并处理IE和Firefox之间的差异。通过深入研究这些文件,开发者可以...
通信网是信息技术领域中的核心部分,它连接了世界各地的人们,使得信息的传输变得迅速而高效。《通信网基本概念及其主体结构》第二版是一本深入探讨这一主题的专业书籍,其课后练习答案则为学习者提供了理解通信网...
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.
1-1 In UMTS,what’s the difference between AAL2 and AAL5? 1-2 In UMTS,what do FDD and TDD mean? 1-3 Describe the processor of hardover and soft handover? 1-4 Describe the spread mechanism used in UMTS...
The reliance investors put on such quantitative analysis was catastrophic for the economy, setting off the ongoing string of financial crises that began with the mortgage market in 2007 and continues...
在虚拟化领域,VMware 是一个非常知名的提供商,其产品线包括了 ESX 和 ESXi 服务器。这两款产品都是为了帮助企业构建、管理和扩展虚拟化环境而设计的,但它们之间存在显著的区别。以下是 VMware ESX 服务器和 ESXi ...
A final difference is the heavy capitalistic and marketeering influence in many of the chapters. This unplanned emphasis is at center stage in sections by Grant, Williams, Brown, and others, and ...