-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
local function only_spaces_or_tabs(text) | ||
for i = 1, #text do | ||
local c = text:sub(i, i) | ||
if c ~= ' ' and c ~= '\t' then | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
|
||
local function only_spaces_or_tabs_regex(text) | ||
return not text:find('[^ \t]') | ||
end | ||
|
||
local function benchmark(func, text, iterations) | ||
local start_time = os.clock() | ||
for _ = 1, iterations do | ||
func(text) | ||
end | ||
local end_time = os.clock() | ||
return end_time - start_time | ||
end | ||
|
||
local text = ' \t\t ' -- Example text to test | ||
local iterations = 1000000 -- Number of iterations for the benchmark | ||
|
||
local time1 = benchmark(only_spaces_or_tabs, text, iterations) | ||
local time2 = benchmark(only_spaces_or_tabs_regex, text, iterations) | ||
|
||
print('Time for only_spaces_or_tabs:', time1) | ||
print('Time for only_spaces_or_tabs_regex:', time2) | ||
|
||
-- Time for only_spaces_or_tabs: 0.07258 | ||
-- Time for only_spaces_or_tabs_regex: 0.093389 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters