#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;

# Variable names declaration

my (%NUCLEOTIDES, $dna_seq, $seqlen, @sequence, @tests, @exectime);

# Initializing variables

         #           1         2         5         4         5
         #  12345678901234567890123456789012345678901234567890
$dna_seq = "ATGCATTGGGGAACCCTGTGCGGATTCTTGTGGCTTTGGCCCTATCTTTT";

@tests = (1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000, 500000);

@exectime = (new Benchmark);


##### EVALUATING STRING-BASED IMPLEMENTATION 

print STDOUT "\n#---> STRING-BASED\n\n";

foreach my $size (@tests) {

	my $dnaseq = $dna_seq x $size;

    # Initializing variables

	%NUCLEOTIDES = ();

    # Looping throught the sequence

	push @exectime, (new Benchmark); # current execution time

	$seqlen = length($dnaseq);

	for (my $i = 0; $i < $seqlen; $i++) {

		my $char = substr($dnaseq,$i,1);

		$NUCLEOTIDES{$char}++;

	};

	push @exectime, (new Benchmark); # current execution time

    # Printing results

	print STDOUT "\t#";
	foreach my $nucleotide (sort keys %NUCLEOTIDES) {

		printf STDOUT "\t%s = %8d", $nucleotide, $NUCLEOTIDES{$nucleotide};

	};

	print STDOUT "\n\tSTRING-BASED execution time : ",
                 sprintf("%8dX ", $size), sprintf("%8dbp ", $size * 50),
	             timestr(timediff($exectime[$#exectime],$exectime[($#exectime - 1)])),"\n";

}; # for $size


##### EVALUATING ARRAY-BASED IMPLEMENTATION 

print STDOUT "\n#---> ARRAY-BASED\n\n";

foreach my $size (@tests) {

	my $dnaseq = $dna_seq x $size;

    # initializing variables

	%NUCLEOTIDES = ();

    # Looping throught the sequence

	push @exectime, (new Benchmark); # current execution time

	@sequence = split //, $dnaseq;

	$seqlen = scalar(@sequence);

	for (my $i = 0; $i < $seqlen; $i++) {

		$NUCLEOTIDES{$sequence[$i]}++;

	};

	push @exectime, (new Benchmark); # current execution time

    # Printing results

	print STDOUT "\t#";
	foreach my $nucleotide (sort keys %NUCLEOTIDES) {

		printf STDOUT "\t%s = %8d", $nucleotide, $NUCLEOTIDES{$nucleotide};

	};

	print STDOUT "\n\tARRAY-BASED execution time : ",
                 sprintf("%8dX ", $size), sprintf("%8dbp ", $size * 50),
	             timestr(timediff($exectime[$#exectime],$exectime[($#exectime - 1)])),"\n";

}; # for $size


## EOF
print STDOUT "\n";
exit(0);
