[fxruby-users] speeding up image.rb

Joel VanderWerf vjoel at PATH.Berkeley.EDU
Sat Nov 13 21:29:00 EST 2004



It's not really important, since image.rb is just a demo, but I was 
tired of waiting for it to start up when I was playing around with it, 
so I made the changes below, and got the following speed up:

$ time ruby-stable-snapshot image2.rb
ruby-stable-snapshot image2.rb  0.79s user 0.12s system 38% cpu 2.377 total
$ time ruby-stable-snapshot image.rb
ruby-stable-snapshot image.rb  6.97s user 0.80s system 84% cpu 9.223 total
$ diff -u image.rb image2.rb
--- image.rb    2004-10-25 17:38:35.000000000 -0700
+++ image2.rb   2004-11-13 18:22:24.000000000 -0800
@@ -114,26 +114,32 @@
      @picture = FXBMPImage.new(getApp(), nil, IMAGE_SHMI|IMAGE_SHMP, 
850, 600)

      # Fill up the color-ramp byte arrays (strings)
-    (0...512).each { |x|
-      (0...50).each { |y|
-        @grey.data[3*(y*512+x)  ]  = (x/2)
-        @grey.data[3*(y*512+x)+1]  = (x/2)
-        @grey.data[3*(y*512+x)+2]  = (x/2)
-      }
-      (0...50).each { |y|
-        @red.data[3*(y*512+x)  ]   = (x/2)
-        @red.data[3*(y*512+x)+1]   = 0
-        @red.data[3*(y*512+x)+2]   = 0
-      }
-      (0...50).each { |y|
-        @green.data[3*(y*512+x)  ] = 0
-        @green.data[3*(y*512+x)+1] = (x/2)
-        @green.data[3*(y*512+x)+2] = 0
-      }
-      (0...50).each { |y|
-        @blue.data[3*(y*512+x)  ]  = 0
-        @blue.data[3*(y*512+x)+1]  = 0
-        @blue.data[3*(y*512+x)+2]  = (x/2)
+    grey_data   = @grey.data
+    red_data    = @red.data
+    green_data  = @green.data
+    blue_data   = @blue.data
+
+    (0...50).each { |y|
+      y_times_512 = y*512
+      (0...512).each { |x|
+        i = 3*(y_times_512+x)
+        x_div_2 = x/2
+
+        grey_data[i  ]  = x_div_2
+        grey_data[i+1]  = x_div_2
+        grey_data[i+2]  = x_div_2
+
+        red_data[i  ]   = x_div_2
+        #red_data[i+1]   = 0
+        #red_data[i+2]   = 0
+
+        #green_data[i  ] = 0
+        green_data[i+1] = x_div_2
+        #green_data[i+2] = 0
+
+        #blue_data[i  ]  = 0
+        #blue_data[i+1]  = 0
+        blue_data[i+2]  = x_div_2
        }
      }



More information about the fxruby-users mailing list