码迷,mamicode.com
首页 > 其他好文 > 详细

Perl 笔试题1

时间:2020-02-27 20:55:21      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:height   a*   int   结果   i++   spl   name   sso   string   

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 %

Perl 笔试题1

标签:height   a*   int   结果   i++   spl   name   sso   string   

原文地址:https://www.cnblogs.com/lyc-seu/p/12374066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!