Perl 笔试题1

让人想犯罪 __ 提交于 2020-02-27 20:19:01

Nvidia 2019 笔试题

源代码

#!/usr/bin/perl

open(IN,"<nv_data1.txt") or die "File does not exist!";
while (<IN>){
    chomp;                    #删除\n
    print("$_\n");
    if($i==0){next};          #跳过第一行
    ($power,$area) = calc($_);#统计当前行单元的面积和功耗
    
    $total_power += $power;   #统计总面积和功耗
    $total_area += $area;
    if(/^ALU/i){
    $alu_power +=  $power;
    } 
    elsif(/^RAM/i){
        $ram_area +=  $area;        
    }
}continue{
    $i++;
}
# 功耗和面积计算函数
sub calc{
    my $string = scalar($_);
    @tmp = split(/\s+/,$string); #根据空格划分单元格内容

    return ($tmp[1]*$tmp[3], $tmp[1]*$tmp[2]);
}
close IN;

#输出
print("Total area is $total_area\n");
print("Total power is $total_power\n");
print("RAM area is $ram_area\n");
print("ALU power is $alu_power\n");

$RAM_area_percentage = $ram_area/$total_area*100;
$ALU_power_percentage = $alu_power/$total_power*100;
print("The percentage of RAM area is $RAM_area_percentage %\n");
print("The percentage of ALU power is $ALU_power_percentage %\n");

程序输出结果

Name:            Instance_number        Area-per-instance      Power-per-instance
ALU-Adder:       32                     945                    0.0333
ALU-Multiplier   16                     6347                   0.2235
RAM-Small        8                      2239                   0.0542
RAM-Large        2                      10943                  1.9312
Control          1                      8345                   0.2344
Data-pipe        1                      10123                  1.3423
Processor        1                      24231                  1.0212
Total area is 214289
Total power is 11.5355
RAM area is 39798
ALU power is 4.6416
The percentage of RAM area is 18.5721152275665 %
The percentage of ALU power is 40.2375276320922 %
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!