<commit_before>#--
#	Rubygame -- Ruby code and bindings to SDL to facilitate game creation
#	Copyright (C) 2004-2007  <NAME>
#
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#	Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with this library; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#++


module Rubygame


  # Clock provides class methods for tracking running time and delaying
  # execution of the program for specified time periods. This is used to
  # provide a consistent framerate, prevent the program from using
  # all the processor time, etc.
  # 
  # Clock also provides instance methods to make it convenient to
  # monitor and limit application framerate. See #tick.
  # 
  class Clock

    # The runtime when the Clock was initialized.
    attr_reader :start

    # The number of times #tick has been called.
    attr_reader :ticks


    # Granularity used for framerate limiting delays in #tick.
    # See #tick and Clock.delay. Default 12.
    attr_accessor :granularity

    # Yield time used for framerate limiting delays in #tick.
    # See #tick and Clock.delay. Default false.
    attr_accessor :yield


    # Create a new Clock instance.
    def initialize()
      @start = Clock.runtime()
      @last_tick = @start
      @ticks = 0

      @target_frametime = nil

      @granularity = 12
      @yield = false

      yield self if block_given?
    end


    # Calibrate @granularity to an appropriate value for this
    # computer, to minimize CPU usage without reducing accuracy.
    # See #tick and Clock.delay for more information about
    # granularity.
    # 
    # The calibration process takes 0.5 seconds by default, but
    # that can be changed by providing a different value for
    # +test_length+. Long tests provide more accurate results.
    # 
    #--
    # 
    # I'm not 100% sure that this is a valid way to measure
    # granularity, or that the granularity of ruby sleep is
    # always the same as that of SDL_Delay. But it can be
    # improved later if needed...
    #
    #++
    def calibrate_granularity( test_length = 0.5 )
      samples = []

      end_time = Time.now + test_length

      while( Time.now < end_time )<commit_msg>Added usage note for calibrate_granularity.<commit_after>#--
#	Rubygame -- Ruby code and bindings to SDL to facilitate game creation
#	Copyright (C) 2004-2007  <NAME>
#
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#	Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with this library; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#++


module Rubygame


  # Clock provides class methods for tracking running time and delaying
  # execution of the program for specified time periods. This is used to
  # provide a consistent framerate, prevent the program from using
  # all the processor time, etc.
  # 
  # Clock also provides instance methods to make it convenient to
  # monitor and limit application framerate. See #tick.
  # 
  class Clock

    # The runtime when the Clock was initialized.
    attr_reader :start

    # The number of times #tick has been called.
    attr_reader :ticks


    # Granularity used for framerate limiting delays in #tick.
    # See #tick and Clock.delay. Default 12.
    attr_accessor :granularity

    # Yield time used for framerate limiting delays in #tick.
    # See #tick and Clock.delay. Default false.
    attr_accessor :yield


    # Create a new Clock instance.
    def initialize()
      @start = Clock.runtime()
      @last_tick = @start
      @ticks = 0

      @target_frametime = nil

      @granularity = 12
      @yield = false

      yield self if block_given?
    end


    # Calibrate @granularity to an appropriate value for this
    # computer, to minimize CPU usage without reducing accuracy.
    # See #tick and Clock.delay for more information about
    # granularity.
    # 
    # The calibration process takes 0.5 seconds by default, but
    # that can be changed by providing a different value for
    # +test_length+. Long tests provide more accurate results.
    # 
    # You usually only need to call this once at the start of your
    # application. You should not run any other ruby threads at the
    # same time, as that will skew the calibration.
    # 
    #--
    # 
    # I'm not 100% sure that this is a valid way to measure
    # granularity, or that the granularity of ruby sleep is
    # always the same as that of SDL_Delay. But it can be
    # improved later if needed...
    #
    #++
    def calibrate_granularity( test_length = 0.5 )
      samples = []

      end_time = Time.now + test_length

      while( Time.now < end_time )
{'fraction_non_alphanumeric': 0.06013986013986014, 'fraction_numerical': 0.012587412587412588, 'mean_word_length': 2.9265614275909404, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7422129', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1526, 'n_words': 762}