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

# Variable names declaration

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

# Initializing variables

$dna_seq = "ATGCATTGGGGAACCCTGTGCGGATTCTTGTGGCTTTGGCCCTATCTTTTCTATGTCCAAGCTG".
           "TGCCCATCCAAAAAGTCCAAGATGACACCAAAACCCTCATCAAGACAATTGTCACCAGGATCAA";
$dna_seq x= 10000; # making a longer sequence to increase execution time to benchmark

@exectime = (new Benchmark);


##### EVALUATING STRING-BASED IMPLEMENTATION 

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

# Initializing variables

%NUCLEOTIDES = ();

# Looping throught the sequence

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

$seqlen = length($dna_seq);

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

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

    $NUCLEOTIDES{$char}++;

};

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

# Printing results

foreach my $nucleotide (sort keys %NUCLEOTIDES) {

    print STDOUT "\tTotal $nucleotide = $NUCLEOTIDES{$nucleotide}\n";

};

print STDOUT "\n\tSTRING-BASED execution time: ",
             timestr(timediff($exectime[$#exectime],$exectime[($#exectime - 1)])),"\n";


##### EVALUATING ARRAY-BASED IMPLEMENTATION 

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

# initializing variables

%NUCLEOTIDES = ();

# Looping throught the sequence

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

@sequence = split //, $dna_seq;

$seqlen = scalar(@sequence);

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

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

};

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

# Printing results

foreach my $nucleotide (sort keys %NUCLEOTIDES) {

    print STDOUT "\tTotal $nucleotide = $NUCLEOTIDES{$nucleotide}\n";

};

print STDOUT "\n\tARRAY-BASED execution time: ",
             timestr(timediff($exectime[$#exectime],$exectime[($#exectime - 1)])),"\n";

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