Static vs Dynamic speed for classes - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

209


76
 
1
class foo;
2
  static task static_add(int a, int b, output int c);
3
    c = a + b;
4
  endtask
5
  
6
  task nonstatic_add(int a, int b, output int c);
7
    c = a + b;
8
  endtask
9
  
10
  static task do_test();
11
    foo handle = new();
12
    longint N = 500000000; // iterations
13
    longint M = 2; // rounds
14
    $display("Test static");
15
    repeat(M) begin
16
      $system("echo $(($(date +%s%N)/1000000)) > ./st"); // that's how we get milliseconds
17
      repeat(N) begin
18
        int c;
19
        foo::static_add(1, 2, c); 
20
      end
21
      $system("echo $(($(date +%s%N)/1000000)) > ./end");
22
      $system("s=`cat ./st`; e=`cat ./end`; echo `expr $e - $s`");
23
    end
24
    
25
    $display("Test automatic");
26
    repeat(M) begin
27
      $system("echo $(($(date +%s%N)/1000000)) > ./st");
28
      repeat(N) begin
29
        int c;
30
        handle.nonstatic_add(1, 2, c);
31
      end
32
      $system("echo $(($(date +%s%N)/1000000)) > ./end");
33
      $system("s=`cat ./st`; e=`cat ./end`; echo `expr $e - $s`");
34
    end
35
  endtask
36
endclass
37
38
module test();
39
  
40
  foo handle = new();
41
 
42
  task automatic do_test();
43
    longint N = 500000000; // iterations
44
    longint M = 2; // rounds
45
    $display("Test static");
46
    repeat(M) begin
47
      $system("echo $(($(date +%s%N)/1000000)) > ./st"); // that's how we get milliseconds
48
      repeat(N) begin
49
        int c;
50
        foo::static_add(1, 2, c); 
51
      end
52
      $system("echo $(($(date +%s%N)/1000000)) > ./end");
53
      $system("s=`cat ./st`; e=`cat ./end`; echo `expr $e - $s`");
54
    end
55
    
56
    $display("Test non-static");
57
    repeat(M) begin
58
      $system("echo $(($(date +%s%N)/1000000)) > ./st");
59
      repeat(N) begin
60
        int c;
61
        handle.nonstatic_add(1, 2, c);
62
      end
63
      $system("echo $(($(date +%s%N)/1000000)) > ./end");
64
      $system("s=`cat ./st`; e=`cat ./end`; echo `expr $e - $s`");
65
    end
66
  endtask
67
  
68
  initial begin
69
    automatic bit is_module = 0;
70
    if (is_module)
71
      do_test();
72
    else
73
      foo::do_test();
74
  end
75
    
76
endmodule
xxxxxxxxxx
1
 
1
// Code your design here
2
47 views and 0 likes     
A short description will be helpful for you to remember your playground's details
 
100:0