Skip to content

Instantly share code, notes, and snippets.

@Souravgoswami
Last active January 30, 2021 08:31
Show Gist options
  • Save Souravgoswami/7f9b4ae2018d58267d7a8142c8ed99fa to your computer and use it in GitHub Desktop.
Save Souravgoswami/7f9b4ae2018d58267d7a8142c8ed99fa to your computer and use it in GitHub Desktop.
Datatype Test
#!/usr/bin/env ruby
$-v = true
TYPES = <<~'EOF'
char % int % short % long % long long
float % double % long double
EOF
N = (ENV[?N] || 100_000).to_i
SRC_DIR = 'cfiles'
if File.exist?(SRC_DIR)
abort "\e[1;31mFile #{SRC_DIR} already exist!\e[0m" if File.file?(SRC_DIR)
else
Dir.mkdir(SRC_DIR)
end
a = ?a
values = N.times.map.with_index { |x, i| "_#{a.next!.dup} = 0" }.join(',')
all_types = TYPES.each_line.map { |x| x.split(?%).each(&:strip!) }.flatten
all_types.each { |type|
IO.write("#{SRC_DIR}/#{type}.c", <<~EOF)
#include <stdio.h>
#include <unistd.h>
unsigned long long calcMemUsage() {
unsigned long long resident, shared ;
FILE *f = fopen("/proc/self/statm", "r") ;
if (fscanf(f, "%*llu %llu %llu", &resident, &shared) != 2) return 0 ;
fclose(f) ;
return (unsigned long long)((resident - shared) * sysconf(_SC_PAGESIZE)) ;
}
int main() {
#{type} #{values} ;
printf("%llu\\n", calcMemUsage()) ;
}
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment