When using ruby-prof, what is the source of `[global]#[no method]` in the profiler output?

199 views Asked by At

Short Version: What is the source of [global]#[no method] in the Ruby profiler output below, and is there any way to eliminate it or reduce its time? It's taking up over 50% of the total execution time for my method and it's the only part of the profiler results that I can't account for.

Long Version: I'm using the ruby-prof gem to profile the Ruby code below. The method find_median_sorted_arrays takes two sorted arrays, and returns their median as a float (hence the name). The problem comes from a coding challenge website, and I decided to run a profiler on my solution because apparently 80% of the submitted Ruby solutions run faster than mine.

Note that the Ruby version is ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin15]:

def find_median_sorted_arrays(nums1, nums2)
  sorted_array = [nil] * (nums1.length + nums2.length)
  nums1_counter, nums2_counter = 0, 0
  sorted_array.each_with_index do |num, index|
    if nums2_counter >= nums2.length || (nums1[nums1_counter] && nums1[nums1_counter] < nums2[nums2_counter])
      sorted_array[index] = nums1[nums1_counter]
      nums1_counter += 1
    else
      sorted_array[index] = nums2[nums2_counter]
      nums2_counter += 1
    end
  end

  return median(sorted_array)
end

def median(array)
  len = array.length
  (array[(len - 1) / 2] + array[len / 2]) / 2.0
end

nums1 = [1,2]
nums2 = [3,4]

RubyProf.start
find_median_sorted_arrays(nums1, nums2)
result = RubyProf.stop

printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)

The output is as follows:

 %self      total      self      wait     child     calls  name
 58.56      0.000     0.000     0.000     0.000        1   [global]#[no method]
 11.60      0.000     0.000     0.000     0.000        1   Array#each
 11.60      0.000     0.000     0.000     0.000        1   Object#find_median_sorted_arrays
  6.63      0.000     0.000     0.000     0.000        1   Array#*
  4.42      0.000     0.000     0.000     0.000        1   Fixnum#/
  4.42      0.000     0.000     0.000     0.000        1   Object#median
  2.76      0.000     0.000     0.000     0.000        1   Enumerable#each_with_index

As I mentioned in the short version, [global]#[no method] is taking up over 50% of the total execution time for my method and it's the only part of the profiler results that I can't account for. Where does this come from, and is there any way to eliminate it or reduce its time?

0

There are 0 answers