title: How to measure Ruby statement, not function, duration
body: <p>I am told that my software runs too slowly in production. To identify the line of code that's slow, I would insert <code>puts</code> statements as shown below, and the logs would indicate timestamps for the output, by looking at the time intervals of the log I can see which statements take the most time.</p>
  
  <p>Is there a better way to go about this? I found some gems that measure at "function" level, but I'm aiming for "statement level" measurements.</p>
  
  <pre><code>def process_api_carts(shop)
    puts "B7BEC BCAA here2.1"
    pager = Pager.new(shop.shopify_client, '/admin/checkouts.json', 'checkouts') do |request|
      request.params['updated_at_min'] = 1.day.ago.to_s(:abandoned_cart_timestamp)
    end
    puts "B7BEC BCAA here2.2"
    carts = pager.to_a
    puts "B7BEC BCAA here2.3"
    carts.sort_by! do |cart|
      Time.zone.parse(cart['created_at'])
    end
    puts "B7BEC BCAA here2.4"
    carts.reverse!
    puts "B7BEC BCAA here2.5"
    # Some carts don't have a customer record attached. Don't know why
    carts.reject! do |cart|
      cart['customer'].nil?
    end
    puts "B7BEC BCAA here2.6"
    carts.uniq! do |cart|
      cart['customer']['id']
    end
    puts "B7BEC BCAA here2.7"
    carts.each do |checkout|
      begin
        puts "B7BEC BCAA here2.8"
        abandoned_cart = shop.abandoned_carts.create(
          body: checkout.to_json,
          shopify_id: checkout['id']
        )
        puts "B7BEC BCAA here2.9"
        shop.events.create(
          topic: Events::CartAbandoned::TOPIC,
          body: {
            type: 'api',
            abandoned_cart_id: abandoned_cart.id
          }.to_json
        )
        puts "B7BEC BCAA here2.10"
      end
    end
  end
  </code></pre>