cpu_time 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 572a693b78ca1182b320d43191cdb63906e114bb2d49b6ad78cb63f4afe08cac
4
+ data.tar.gz: 3d8babe72ec0e583a38d70625ea8462be92979e10e2c7fc37145da04bb3aab5a
5
+ SHA512:
6
+ metadata.gz: 84785920cfea7287aae296029c903c10ea057246eb12b080226d1fff3acee7d72fd4500a6dad535eb257245813e96d3eb1a9d5b9f7947f170522f7a335a193a4
7
+ data.tar.gz: b2a5140035a5518e2616f2eb69b3950e450cf43302940b6585de3e349de1242542d24412745dd88d230333ca871360b2aeba89cd126dfdb9fd85f4d502b3f840
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Mike Yang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # CPUTime
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cpu_time`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cpu_time.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rake/extensiontask"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ Rake::ExtensionTask.new('cpu_time') do |ext|
10
+ ext.lib_dir = 'lib/cpu_time'
11
+ end
12
+
13
+ task default: :spec
@@ -0,0 +1,50 @@
1
+ #include <ruby.h>
2
+ #include <unistd.h>
3
+ #include <sys/times.h>
4
+ #include <time.h>
5
+
6
+ static inline double cpu_time()
7
+ {
8
+ #if defined(_WIN32)
9
+ FILETIME createTime;
10
+ FILETIME exitTime;
11
+ FILETIME kernelTime;
12
+ FILETIME userTime;
13
+
14
+ ULARGE_INTEGER kernelTimeInt;
15
+ ULARGE_INTEGER userTimeInt;
16
+
17
+ GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime);
18
+
19
+ kernelTimeInt.LowPart = kernelTime.dwLowDateTime;
20
+ kernelTimeInt.HighPart = kernelTime.dwHighDateTime;
21
+ userTimeInt.LowPart = userTime.dwLowDateTime;
22
+ userTimeInt.HighPart = userTime.dwHighDateTime;
23
+
24
+ return (double)(kernelTimeInt.QuadPart + userTimeInt.QuadPart);
25
+ #elif !defined(CLOCK_PROCESS_CPUTIME_ID)
26
+ #include <sys/resource.h>
27
+ struct rusage usage;
28
+ getrusage(RUSAGE_SELF, &usage);
29
+ return usage.ru_stime.tv_sec + usage.ru_utime.tv_sec + ((usage.ru_stime.tv_usec + usage.ru_utime.tv_usec) / 1000000.0);
30
+ #else
31
+ struct timespec clock;
32
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &clock);
33
+ return clock.tv_sec + (clock.tv_nsec / 1000000000.0);
34
+ #endif
35
+ }
36
+
37
+ static VALUE
38
+ rb_cpu_time()
39
+ {
40
+ double time = cpu_time();
41
+
42
+ return rb_float_new(time);
43
+ }
44
+
45
+ void
46
+ Init_cpu_time(void)
47
+ {
48
+ VALUE class = rb_define_class("CPUTimeTimer", rb_cObject);
49
+ rb_define_singleton_method(class, "cpu_time", rb_cpu_time, 0);
50
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('cpu_time/cpu_time')
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CPUTime
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cpu_time.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cpu_time/version"
4
+ require "cpu_time/cpu_time"
5
+
6
+ module CPUTime
7
+ def self.time
8
+ CPUTimeTimer.cpu_time
9
+ end
10
+ end
11
+
12
+ module Kernel
13
+ if method_defined?(:cpu_time)
14
+ puts "cpu_time has been defined, please use CPUTime.time"
15
+ else
16
+ def cpu_time
17
+ CPUTimeTimer.cpu_time
18
+ end
19
+ end
20
+ end
data/sig/cpu_time.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module CPUTime
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpu_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Yang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Get CPU time.
28
+ email:
29
+ - yfractal@gmail.com
30
+ executables: []
31
+ extensions:
32
+ - ext/cpu_time/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - ext/cpu_time/cpu_time.c
40
+ - ext/cpu_time/extconf.rb
41
+ - lib/cpu_time.rb
42
+ - lib/cpu_time/version.rb
43
+ - sig/cpu_time.rbs
44
+ homepage:
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.5.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Get CPU time.
67
+ test_files: []