Project1
标题: 关于 Array 和 Table 、 [打印本页]
作者: 804173948 时间: 2015-4-11 15:54
标题: 关于 Array 和 Table 、
F1里写的是 “Ruby Array 类在处理大量信息时效率很差,因此使用了此类”
所以说处理起来有什么不同...
为什么Array会效率很差
请大神们举一些例子0.0
作者: taroxd 时间: 2015-4-11 16:11
说实话,我感觉效率差不多
size = 100
# Array
t = Time.now
a_big_array = Array.new(size) { Array.new(size) { Array.new(size) } }
size.times do |x|
size.times do |y|
size.times do |z|
a_big_array[x][y][z] = x + y + z # write
a_big_array[x][y][z] # read
end
end
end
p Time.now - t
# Table
t = Time.now
a_big_table = Table.new(size, size, size)
size.times do |x|
size.times do |y|
size.times do |z|
a_big_table[x, y, z] = x + y + z # write
a_big_table[x, y, z] # read
end
end
end
p Time.now - t
size = 100
# Array
t = Time.now
a_big_array = Array.new(size) { Array.new(size) { Array.new(size) } }
size.times do |x|
size.times do |y|
size.times do |z|
a_big_array[x][y][z] = x + y + z # write
a_big_array[x][y][z] # read
end
end
end
p Time.now - t
# Table
t = Time.now
a_big_table = Table.new(size, size, size)
size.times do |x|
size.times do |y|
size.times do |z|
a_big_table[x, y, z] = x + y + z # write
a_big_table[x, y, z] # read
end
end
end
p Time.now - t
作者: RyanBern 时间: 2015-4-11 19:20
class Mat_Array
attr_reader :n
def initialize(n)
@n = n
@data = Array.new(n){ Array.new(n){ 0 } }
end
def [](i, j)
return @data[i][j]
end
def []=(i, j, value)
@data[i][j] = value
end
def *(other)
result = Mat_Array.new(n)
for i in 0...n
for j in 0...n
for k in 0...n
result[i, j] += self[i, k] + other[k ,j]
end
end
end
end
end
class Mat_Table
attr_reader :n
def initialize(n)
@n = n
@data = Table.new(n, n)
end
def [](i, j)
return @data[i, j]
end
def []=(i, j, value)
@data[i, j] = value
end
def *(other)
result = Mat_Array.new(n)
for i in 0...n
for j in 0...n
for k in 0...n
result[i, j] += self[i, k] + other[k ,j]
end
end
end
end
end
def test_array
t = Time.now
a = Mat_Array.new(100)
b = Mat_Array.new(100)
a * b
p Time.now - t
end
def test_table
t = Time.now
a = Mat_Table.new(100)
b = Mat_Table.new(100)
a * b
p Time.now - t
end
class Mat_Array
attr_reader :n
def initialize(n)
@n = n
@data = Array.new(n){ Array.new(n){ 0 } }
end
def [](i, j)
return @data[i][j]
end
def []=(i, j, value)
@data[i][j] = value
end
def *(other)
result = Mat_Array.new(n)
for i in 0...n
for j in 0...n
for k in 0...n
result[i, j] += self[i, k] + other[k ,j]
end
end
end
end
end
class Mat_Table
attr_reader :n
def initialize(n)
@n = n
@data = Table.new(n, n)
end
def [](i, j)
return @data[i, j]
end
def []=(i, j, value)
@data[i, j] = value
end
def *(other)
result = Mat_Array.new(n)
for i in 0...n
for j in 0...n
for k in 0...n
result[i, j] += self[i, k] + other[k ,j]
end
end
end
end
end
def test_array
t = Time.now
a = Mat_Array.new(100)
b = Mat_Array.new(100)
a * b
p Time.now - t
end
def test_table
t = Time.now
a = Mat_Table.new(100)
b = Mat_Table.new(100)
a * b
p Time.now - t
end
Table比Array快一点,但是二者应该是在一个量级上的。
这里测试Array跑完一次矩阵乘法需要3.5s,Table需要3.0s左右,我特地使用了效率低的写法。
感觉处理这类问题,浪费的时间应该在元素的读取上,如果用改良的写法能提高一倍的效率。
欢迎光临 Project1 (https://rpg.blue/) |
Powered by Discuz! X3.1 |