Created
August 22, 2009 16:27
-
-
Save vikhyat/172834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_matrix(size) | |
Array.new(size) { Array.new(size) { rand 10 } } | |
end | |
# Only for 3x3 matrices for the time being | |
# Refactor! | |
def determinant(matrix) | |
(matrix[0][0] * ((matrix[1][1] * matrix[2][2]) - (matrix[2][1] * matrix[1][2]))) - (matrix[0][1] * ((matrix[1][0] * matrix[2][2]) - (matrix[2][0] * matrix[1][2]))) + (matrix[0][2] * ((matrix[1][0] * matrix[2][1]) - (matrix[2][0] * matrix[1][1]))) | |
end | |
def put_matrix(matrix) | |
matrix.each do |line| | |
print '| ' | |
line.each { |element| print "#{element} " } | |
puts '|' | |
end | |
end | |
def benchmark | |
100000.times do | |
matrix = generate_matrix 3 | |
determinant matrix | |
end | |
end | |
def prog | |
put_matrix(matrix = generate_matrix(3)) | |
puts "Determinant = #{determinant matrix}" | |
end | |
benchmark | |
__END__ | |
$ time ruby matrix.rb | |
real 0m1.014s | |
user 0m0.856s | |
sys 0m0.004s | |
$ time jruby matrix.rb | |
real 0m2.317s | |
user 0m1.972s | |
sys 0m0.128s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment