Skip to content

Instantly share code, notes, and snippets.

@raccy
Created December 26, 2017 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raccy/c7a7cdd9ff61f43768705076a5a737ca to your computer and use it in GitHub Desktop.
Save raccy/c7a7cdd9ff61f43768705076a5a737ca to your computer and use it in GitHub Desktop.
ifが遅いなんて誰が言ったのだろう?
#include <stdio.h>
#include <stdlib.h>
#define LIST_MAX_LINE 10000
#define MAX_LEN 256
int same_char(const char *s, const char *t);
int main(void)
{
char *list = (char *)malloc(LIST_MAX_LINE * MAX_LEN);
int line = LIST_MAX_LINE;
for (int i = 0; i < LIST_MAX_LINE; i++) {
char *result = fgets(list + (i * MAX_LEN), MAX_LEN, stdin);
if (result == NULL) {
line = i + 1;
break;
}
}
int total_count = 0;
for (int i = 0; i < line; i++) {
for (int j = i; j < line; j++) {
total_count += same_char(list + (i * MAX_LEN),
list + (j * MAX_LEN));
}
}
printf("line: %d, total count: %d\n", line, total_count);
free(list);
return 0;
}
# frozen_string_literal: true
require 'benchmark'
def make_test(io)
len = 80
line_len = 10000
list = ('a'..'z').to_a
size = list.size
line_len.times do
str = +''
len.times { str << list[rand(size)] }
io.puts str
end
end
list = %w[
count_eq
count_if
count_if_after
count_if_null
count_r_eq
count_r_if
count_r_if_after
count_r_if_null
]
main_prog = 'count'
system("gcc -O2 -Wall -std=c11 -c #{main_prog}.c -o #{main_prog}.o")
list.each do |name|
system("gcc -O2 -Wall -std=c11 -c #{name}.c -o #{name}.o")
system("gcc -o #{name} #{main_prog}.o #{name}.o")
end
test_text = 'test.txt'
open(test_text, 'w') do |io|
make_test(io)
end
result = nil
Benchmark.bm(16) do |x|
list.each do |name|
current = nil
x.report(name) do
current = `#{name} < #{test_text}`
end
if result
if result != current
raise "not same result... maybe the program have a bug: #{name}.c"
end
else
result = current
end
end
end
puts "result -> #{result}"
int same_char(const char *s, const char *t)
{
int count = 0;
while (*s && *t) {
count += (*s++ == *t++);
}
return count;
}
int same_char(const char *s, const char *t)
{
int count = 0;
while (*s && *t) {
if (*s++ == *t++) count++;
}
return count;
}
int same_char(const char *s, const char *t)
{
int count = 0;
while (*s && *t) {
if (*s == *t) count++;
s++;
t++;
}
return count;
}
int same_char(const char *s, const char *t)
{
int count = 0;
while (*s != '\0' && *t != '\0') {
if (*s == *t) count++;
s++;
t++;
}
return count;
}
int same_char(const char *restrict s, const char *restrict t)
{
int count = 0;
while (*s && *t) {
count += (*s++ == *t++);
}
return count;
}
int same_char(const char *restrict s, const char *restrict t)
{
int count = 0;
while (*s && *t) {
if (*s++ == *t++) count++;
}
return count;
}
int same_char(const char *restrict s, const char *restrict t)
{
int count = 0;
while (*s && *t) {
if (*s == *t) count++;
s++;
t++;
}
return count;
}
int same_char(const char *restrict s, const char *restrict t)
{
int count = 0;
while (*s != '\0' && *t != '\0') {
if (*s == *t) count++;
s++;
t++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment