Skip to content
Browse files

Performance improvement in IO#puts.

* before
      user     system      total        real
puts one char  0.290000   0.400000   0.690000 (  0.683528)

* after
      user     system      total        real
puts one char  0.120000   0.350000   0.470000 (  0.461036)

{{{
require 'benchmark'
require 'tempfile'

file = open('/tmp/puts_benchmark', 'w')
char = 'a'

Benchmark.bm do |x|

  x.report do
    100000.times do
      file.puts char
    end
  end
end
}}}
  • Loading branch information...
1 parent 36f8067 commit a7280cfc168ddd019c732508ad691062bbb4dcfd @Watson1978 Watson1978 committed Jun 25, 2011
Showing with 6 additions and 1 deletion.
  1. +6 −1 io.c
View
7 io.c
@@ -3058,12 +3058,17 @@ rb_io_puts(VALUE out, SEL sel, int argc, VALUE *argv)
return Qnil;
}
for (i = 0; i < argc; i++) {
+ if (TYPE(argv[i]) == T_STRING) {
+ line = argv[i];
+ goto string;
+ }
line = rb_check_array_type(argv[i]);
if (!NIL_P(line)) {
rb_exec_recursive(io_puts_ary, line, out);
continue;
}
line = rb_obj_as_string(argv[i]);
+ string:
rb_io_write(out, line);
if (RSTRING_LEN(line) == 0
|| RSTRING_PTR(line)[RSTRING_LEN(line)-1] != '\n') {
@@ -3100,7 +3105,7 @@ rb_p(VALUE obj) /* for debug print within C code */
VALUE
rb_io_write(VALUE v, VALUE i)
{
- return rb_funcall(v, id_write, 1, i);
+ return rb_vm_call(v, selWrite, 1, &i);
}
/*

0 comments on commit a7280cf

Please sign in to comment.
Something went wrong with that request. Please try again.