From dcff873fcc07e1fa0fd39f07b4ebbe995f31af98 Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Sun, 5 Apr 2020 23:42:28 +0300 Subject: [PATCH] Added count_lens for get_function_len --- get_function_len.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/get_function_len.py b/get_function_len.py index acd271c..ea6e7dc 100644 --- a/get_function_len.py +++ b/get_function_len.py @@ -10,8 +10,8 @@ def get_rank_file_object(args_count, rank_ind): elif os.path.exists(base_name + ".xz"): return lzma.open(base_name + ".xz", 'rb') else: - print("No results file for {} arguments rank {}".format(args_count, rank_ind)) - sys.exit(1) + return None + def is_function_exists(function_number, file_object): file_object.seek(function_number // 8) @@ -29,7 +29,29 @@ def function_values_to_number(values): return res +def count_lens(args_count): + bit_counts = bytes(bin(x).count("1") for x in range(256)) + len_to_count = {} + for rank_ind in range(1, 10): + file_object = get_rank_file_object(args_count, rank_ind) + if not file_object: + break + len_to_count[rank_ind] = sum(map(lambda x: bit_counts[x], file_object.read())) + total = sum(len_to_count.values()) + for rank_ind, count in len_to_count.items(): + print("len {} count {} share {}".format( + rank_ind, + count, + 1. * count / total + )) + print("average: {}".format( + sum([key*value for key,value in len_to_count.items()]) / total + )) + def main(): + if sys.argv[1] == "count_lens": + count_lens(sys.argv[2]) + return args_count = { 8: 3, 16: 4, @@ -38,7 +60,11 @@ def main(): function_number = function_values_to_number(sys.argv[1]) print("Function number is {}".format(function_number)) for rank_ind in range(1, 10): - if is_function_exists(function_number, get_rank_file_object(args_count, rank_ind)): + file_object = get_rank_file_object(args_count, rank_ind) + if not file_object: + print("No results file for {} arguments rank {}".format(args_count, rank_ind)) + sys.exit(1) + if is_function_exists(function_number, file_object): print("Result len is {}".format(rank_ind)) return