Quant-X-Security-Coding-GmbH / svd-challenge

Challenging singular value decomposition examples for the D-Wave quantum annealer
Apache License 2.0
0 stars 0 forks source link

Set up timer to measure execution time of functions #2

Closed XeniaGabriela closed 3 years ago

XeniaGabriela commented 3 years ago

Create file timer.py with

import time

class TimerError(Exception):

    """A custom exception used to report errors in use of Timer class"""

class Timer:
    def __init__(self):
        self._start_time = None

    def start(self):
        """Start a new timer"""
        if self._start_time is not None:
            raise TimerError(f"Timer is running. Use .stop() to stop it")
        self._start_time = time.perf_counter()

    def stop(self):
        """Stop the timer, and report the elapsed time"""
        if self._start_time is None:
            raise TimerError(f"Timer is not running. Use .start() to start it")

        elapsed_time = time.perf_counter() - self._start_time
        self._start_time = None
        print(f"Elapsed time: {elapsed_time:0.4f} seconds")
XeniaGabriela commented 3 years ago

Example on how to use the Timer() class in another class:

# -*- coding: utf-8 -*-
from timer import Timer
from config import config
from bin_map import BinGenome
from pathlib import Path

class AlgebraicGenome(object):

    def __init__(self):
        self.timer = Timer()
        self.binary_4base = BinGenome().binary_4base_dict

    # TODO: Find mistake, where is a 4 fold repetition????
    def read_genome(self, text_file):
        self.timer.start()
        genome_lines = []
        genome_bytes = b""

        with open(text_file, "r") as f:
            four_line_string = ""
            line_number = 0

            # iterate through lines in text file (line end defined by \n)
            for line in f:

                # omit first line with file declaration
                if not line[0] == ">" and len(line) > 3:
                    line_number += 1
                    four_line_string += line.rstrip()

                if line_number % 4 == 0:
                    genome_lines.append(four_line_string)
                    four_line_string = ""

            print(len(genome_lines))

            for genome_line in genome_lines:
                char_pos = 0

                # add converted character groups to binary string
                while char_pos < len(genome_line):
                    character_quadruple = genome_line[char_pos:char_pos + 4]
                    genome_bytes += self.binary_4base[character_quadruple]
                    char_pos += 4

            # write bytes to binary file
            with open(Path(str(text_file).replace('.fa', '') + '_binary.fa'), 'wb') as new_file:
                print(new_file)
                new_file.write(genome_bytes)
                new_file.close()

        self.timer.stop()
        return genome_bytes
XeniaGabriela commented 3 years ago

Implemented with commit https://github.com/Quant-X-Security-Coding-GmbH/svd-challenge/commit/aedf50149b2e72f6ce576ec76b33a3a5c08f1eee