<commit_before>//
//  Timing.swift
//
//  Created by <NAME> on 2014-06-24.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin.Mach.mach_time
import Darwin.C.time
import AppKit.AppKitDefines

/**
  A struct whose purpose is to pretty-print short durations of time.
*/

public struct Interval: CustomStringConvertible
{
  var ns: Int64

  init(_ nanoseconds: Int64)
  {
    ns = nanoseconds
  }

  init(_ nanoseconds: Int)
  {
    ns = Int64(nanoseconds)
  }

  init(_ nanoseconds: Double)
  {
    ns = Int64(nanoseconds)
  }

  init(seconds: CFTimeInterval)
  {
    ns = Int64(seconds*1e9)
  }

  public var description: String
    {
      if abs(ns) > 5_000_000_000
      { // over 5 seconds: round to milliseconds, display as seconds
        return (Double(ns/1_000_000)/1e3).description + " s"
      }
      if abs(ns) >= 1_000_000_000
      { // over 1 second: round to 10s of µs, display as milliseconds
        return (Double(ns/10_000)/1e2).description + " ms"
      }
      if abs(ns) >= 100_000
      { // round to microseconds, display as milliseconds
        return (Double(ns/1000)/1e3).description + " ms"
      }
      if abs(ns) >= 1_000
      {
        // otherwise display as microseconds
        return (Double(ns)/1e3).description + " µs"
      }
      return ns.description + " ns"
  }
  
  public var interval: CFTimeInterval
  {
    return Double(ns)*1e-9
  }
}

func / (dt: Interval, n: Int) -> Interval
{
  return Interval(dt.ns/Int64(n))
}

/**
  Timing-related utility based on mach_absolute_time().
*/

public struct Time: CustomStringConvertible
{
  private let t: Int64

  public init()
  {
    t = Int64(mach_absolute_time())
  }

  /**
    offset: offset in seconds between the uptime and a timestamp that can be mapped to a date
    This is not a constant, strictly speaking. Probably close enough, though.
  */

  private static var offset: CFTimeInterval = { CFAbsoluteTimeGetCurrent() - CACurrentMediaTime() }()

  /**
    scale: how to scale from the mach timebase to nanoseconds. See Technical Q&A QA1398
  */

  private static var scale: mach_timebase_info = {
    var info = mach_timebase_info(numer: 0, denom: 0)
    mach_timebase_info(&info)
    return info
  }()

  /**
    An analog to the MATLAB tic ... toc time measurement function pair.
    example:
    let tic = Time()
    println(tic.toc)
  */

  public var toc: Interval
  {
    let dt = (Time().t - t) * Int64(Time.scale.numer)/Int64(Time.scale.denom)
    return Interval(dt)
  }

  public var nanoseconds: Int64
  {
    return t * Int64(Time.scale.numer)/Int64(Time.scale.denom)
  }

  public var absoluteTime: CFAbsoluteTime
  {
      return Double(self.nanoseconds)*1e-9 + Time.offset
  }
  
  public var description: String
  {
    return NSDate(timeIntervalSinceReferenceDate: absoluteTime).description
  }
}

extension Time
{
  public static func Now() -> Time { return Time() }

  /**
    Time.Since(t: Time) returns an Interval.
    example:
    let starttime = Time()
    println(Time.Since(starttime))
  */

  public static func Since(tic: Time) -> Interval
  {
    return tic.toc
  }
}

/**
  Sleep the current thread for an interval of time.
*/

extension Time
{
  public static func Wait(interval: Interval)
  {
    if interval.ns >= 0
    {
      var timeRequested = timespec(tv_sec: Int(interval.ns/1_000_000_000), tv_nsec: Int(interval.ns%1_000_000_000))
      while nanosleep(&timeRequested, &timeRequested) == -1 {}
    }
  }

  public static func Wait(ms: Int)
  {
    if ms >= 0
    {
      Time.Wait(Double(ms)/1000)
    }
  }

  public static func Wait(ms: UInt32)
  {
    Time.Wait(Int(ms))
  }

  public static func Wait(seconds: CFTimeInterval)
  {
    if seconds > 0
    {
      let wholeseconds = floor(seconds)
      var timeRequested = timespec(tv_sec: Int(wholeseconds), tv_nsec: Int((seconds-wholeseconds)*1_000_000_000))
      while nanosleep(&timeRequested, &timeRequested) == -1 {}
    }
  }
}<commit_msg>Clean up<commit_after>//
//  Timing.swift
//
//  Created by <NAME> on 2014-06-24.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin.Mach.mach_time
import Darwin.C.time
import Foundation.NSDate
import QuartzCore

/**
  A struct whose purpose is to pretty-print short durations of time.
*/

public struct Interval: CustomStringConvertible
{
  let ns: Int64

  init(_ nanoseconds: Int64)
  {
    ns = nanoseconds
  }

  init(_ nanoseconds: Int)
  {
    ns = Int64(nanoseconds)
  }

  init(_ nanoseconds: Double)
  {
    ns = Int64(nanoseconds)
  }

  init(seconds: NSTimeInterval)
  {
    ns = Int64(seconds*1e9)
  }

  public var description: String
    {
      if abs(ns) > 5_000_000_000
      { // over 5 seconds: round to milliseconds, display as seconds
        return (Double(ns/1_000_000)/1e3).description + " s"
      }
      if abs(ns) >= 1_000_000_000
      { // over 1 second: round to 10s of µs, display as milliseconds
        return (Double(ns/10_000)/1e2).description + " ms"
      }
      if abs(ns) >= 100_000
      { // round to microseconds, display as milliseconds
        return (Double(ns/1000)/1e3).description + " ms"
      }
      if abs(ns) >= 1_000
      {
        // otherwise display as microseconds
        return (Double(ns)/1e3).description + " µs"
      }
      return ns.description + " ns"
  }
  
  public var interval: NSTimeInterval
  {
    return Double(ns)*1e-9
  }
}

func / (dt: Interval, n: Int) -> Interval
{
  return Interval(dt.ns/Int64(n))
}

/**
  Timing-related utility based on mach_absolute_time().
  It seems correct on Mac OS X. It might not be on iOS.
*/

public struct Time: CustomStringConvertible
{
  private let t: Int64

  public init()
  {
    t = Int64(mach_absolute_time())
  }

  /**
    offset: offset in seconds between the uptime and a timestamp that can be mapped to a date
    This is not a constant, strictly speaking. Probably close enough, though.
  */

  private static var offset: NSTimeInterval = { CFAbsoluteTimeGetCurrent() - CACurrentMediaTime() }()

  /**
    scale: how to scale from the mach timebase to nanoseconds. See Technical Q&A QA1398
  */

  private static var scale: mach_timebase_info = {
    var info = mach_timebase_info(numer: 0, denom: 0)
    mach_timebase_info(&info)
    return info
  }()

  /**
    An analog to the MATLAB tic ... toc time measurement function pair.
    example:
    let tic = Time()
    println(tic.toc)
  */

  public var toc: Interval
  {
    return Time() - self
  }

  public var nanoseconds: Int64
  {
    return t * Int64(Time.scale.numer)/Int64(Time.scale.denom)
  }

  public var absoluteTime: NSTimeInterval
  {
      return Double(self.nanoseconds)*1e-9 + Time.offset
  }

  public var description: String
  {
    return NSDate(timeIntervalSinceReferenceDate: absoluteTime).description
  }
}

public func -(time1: Time, time2: Time) -> Interval
{
  return Interval((time1.t - time2.t) * Int64(Time.scale.numer)/Int64(Time.scale.denom))
}

extension Time
{
  /**
    Time.Since(t: Time) returns an `Interval`.
    example:
    ```
    let starttime = Time()
    print(Time.Since(starttime))
    ```
  */

  public static func Since(startTime: Time) -> Interval
  {
    return Time() - startTime
  }
}

/**
  Sleep the current thread for an interval of time.
*/

public struct Thread
{
  public static func Sleep(interval: Interval)
  {
    if interval.ns >= 0
    {
      var timeRequested = timespec(tv_sec: Int(interval.ns/1_000_000_000), tv_nsec: Int(interval.ns%1_000_000_000))
      while nanosleep(&timeRequested, &timeRequested) == -1 {}
    }
  }

  public static func Sleep(ms: Int)
  {
    if ms >= 0
    {
      Sleep(Double(ms)/1000)
    }
  }

  public static func Sleep(ms: UInt32)
  {
    Sleep(Int(ms))
  }

  public static func Sleep(seconds: NSTimeInterval)
  {
    if seconds > 0
    {
      let wholeseconds = floor(seconds)
      var timeRequested = timespec(tv_sec: Int(wholeseconds), tv_nsec: Int((seconds-wholeseconds)*1_000_000_000))
      while nanosleep(&timeRequested, &timeRequested) == -1 {}
    }
  }
}
{'fraction_non_alphanumeric': 0.11416832669322709, 'fraction_numerical': 0.0375996015936255, 'mean_word_length': 2.7380176826430898, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '2189818', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2733, 'n_words': 722}