Oracle中的GET_TIME

时间:2016-03-07 10:05:51

标签: oracle plsql oracle11g

我有一个代码,应该给我程序的编译时间,但它只给我0秒!计算错了吗?还有其他办法吗?

    create or replace PROCEDURE proc_time AS
    v NUMBER := 1;
    x NUMBER := 0;
    counter number := 0;
    summ NUMBER;
    ex_start NUMBER;
    ex_end NUMBER;
    ex_time NUMBER;
    BEGIN
    ex_start := dbms_utility.get_time;
    while  counter <= 19
    loop  
        counter := counter + 1;
        summ := x+v;    
        x := v;
        V := summ;  
        dbms_output.put_line('Fibonacci nr'||counter||': '||summ);
    END loop;
  ex_end := DBMS_UTILITY.GET_TIME;
  ex_time := (ex_end-ex_start)/100; 
  DBMS_OUTPUT.PUT_LINE( 'Exekveringstid: ' || ex_time || ' sekunder.' );
END;
/

3 个答案:

答案 0 :(得分:1)

  • 也许,它的执行速度太快了。你可以尝试将循环而不是19的数量增加到更大的数量。

  • 此外,它会为您提供执行时间 e,而不是编译时间。该过程已编译并存储在数据库中。输出中的时间是在运行时,即执行过程时。

例如,

SQL> CREATE OR REPLACE PROCEDURE test_time
  2  AS
  3    l_start  NUMBER;
  4    l_loops  NUMBER := 10000000;
  5    l_number NUMBER := 0;
  6  BEGIN
  7    l_start := DBMS_UTILITY.get_time;
  8    FOR i IN 1 .. l_loops
  9    LOOP
 10      l_number := l_number + i;
 11    END LOOP;
 12    DBMS_OUTPUT.put_line('time taken: ' || (DBMS_UTILITY.get_time - l_start) || ' hsecs');
 13  END test_time;
 14  /

Procedure created.

SQL> set serveroutput on
SQL> BEGIN
  2     test_time;
  3  END;
  4  /
time taken: 101 hsecs

PL/SQL procedure successfully completed.

SQL>

答案 1 :(得分:1)

作为替代使用TIMESTAMP数据类型,这支持最多纳秒(假设您的服务器也支持它)

DECLARE
    ex_start TIMESTAMP(9);  
    Duration INTERVAL DAY TO SECOND;

    v        NUMBER := 1;
    x        NUMBER := 0;
    counter  NUMBER := 0;
    summ     NUMBER;

BEGIN
    ex_start := LOCALTIMESTAMP;  
    WHILE counter <= 10000 LOOP
        counter := counter + 1;
        summ    := x       +v;
        x       := v;
        V       := summ;
    END LOOP;

    Duration :=  LOCALTIMESTAMP - ex_start;
    DBMS_OUTPUT.PUT_LINE ( EXTRACT(SECOND FROM Duration) /1000||' msec.');
END;

答案 2 :(得分:0)

  

正如Lalit在上面的回答中所解释的那样。你的代码片段运行得太快了   计算时差。尝试增加while循环   一些更大的数字,在这里你去!

CREATE OR REPLACE PROCEDURE proc_time
AS
  v        NUMBER := 1;
  x        NUMBER := 0;
  counter  NUMBER := 0;
  summ     NUMBER;
  ex_start NUMBER;
  ex_end   NUMBER;
  ex_time  NUMBER;
BEGIN
  ex_start := dbms_utility.get_time;
  dbms_output.put_line('start time ==>'||NVL(ex_start,0));
  WHILE counter <= 190000
  LOOP
    counter := counter + 1;
    summ    := x       +v;
    x       := v;
    V       := summ;
    --        dbms_output.put_line('Fibonacci nr'||counter||': '||summ);
  END LOOP;
  ex_end := DBMS_UTILITY.GET_TIME;
  dbms_output.put_line('End time ==>'||ex_end);
  ex_time := (ex_end-ex_start);
  DBMS_OUTPUT.PUT_LINE( 'Exekveringstid: ' || ex_time || ' sekunder.' );
END;

set serveroutput on;
exec proc_time;

-------------------------------OUTPUT----------------------------------------

start time ==>1761607275
End time ==>1761607281
Exekveringstid: 6 sekunder.