Explorar el Código

feat: 增加计时功能

启动hellovm添加-p参数可以启动计时功能

link #7
SongZihuan hace 4 años
padre
commit
a895d8e4f9
Se han modificado 8 ficheros con 27 adiciones y 11 borrados
  1. 7 3
      argument/argument.c
  2. 1 0
      include/arguement.h
  3. 1 1
      main.c
  4. 3 3
      src/virtualmath.c
  5. 6 1
      vmcore/include/macro.h
  6. 1 1
      vmcore/include/run.h
  7. 7 1
      vmcore/src/run.c
  8. 1 1
      vmcore/src/runfile.c

+ 7 - 3
argument/argument.c

@@ -8,13 +8,14 @@ static const struct option long_option[]={
         {"stderr",required_argument,NULL,'o'},
         {"stdout",required_argument,NULL,'e'},
         {"stdin",required_argument,NULL,'i'},
-        {"not-run-cl",required_argument,NULL,'n'},
+        {"not-run-cl",no_argument,NULL,'n'},
+        {"print-clock",no_argument,NULL,'p'},
         {NULL,0,NULL,0}
 };
 
-static const char *short_option = "o:e:i:n";
+static const char *short_option = "o:e:i:np";
 
-Args args = {.out_file=NULL, .error_file=NULL, .in_file=NULL, .run_commandLine=true};
+Args args = {.out_file=NULL, .error_file=NULL, .in_file=NULL, .run_commandLine=true, .p_clock=false};
 
 /**
  * 参数设置, args是全局结构体, 保存全局的参数设置
@@ -44,6 +45,9 @@ int getArgs(const int argc, char **argv)
             case 'n':
                 args.run_commandLine = false;
                 break;
+            case 'p':
+                args.p_clock = true;
+                break;
             case '?':
                 fprintf(stderr, "[Error]: get not success args : -%c\n", (char)optopt);
                 return -1;

+ 1 - 0
include/arguement.h

@@ -8,6 +8,7 @@ struct Args{
     char *out_file;
     char *in_file;
     bool run_commandLine;
+    bool p_clock;
 };
 extern struct Args args;
 typedef struct Args Args;

+ 1 - 1
main.c

@@ -17,7 +17,7 @@ int main(int argc, char *argv[]) {
     }
     memVirtualMathUseJmp = true;
 
-    if (getArgs(argc, argv))  // 命令行参数设定
+    if (getArgs(argc, argv) == -1)  // 命令行参数设定
         return 2;
 
     initVirtualMath();

+ 3 - 3
src/virtualmath.c

@@ -15,9 +15,9 @@ void runCodeFile(Inter *inter, char *file[]) {
                 continue;
         }
         if (runParser(*file, inter, false, &pst)) {
-            globalIterStatement(&result, inter, pst);
+            globalIterStatement(&result, inter, pst, true);
             if (result.type == R_error) {
-                printError(&result, inter, true);
+                printError(&result, inter, args.p_clock);
                 should_break = true;
             }
             freeStatement(pst);
@@ -39,7 +39,7 @@ void runCodeStdin(Inter *inter, char *hello_string) {
             clearerr(stdin);
         fprintf(stdout, ">>> ");
         if (runParser(NULL, inter, true, &pst)) {
-            globalIterStatement(&result, inter, pst);
+            globalIterStatement(&result, inter, pst, false);
             if (result.type == R_error && !(should_break = is_quitExc(result.value, inter)))
                 printError(&result, inter, true);
             freeStatement(pst);

+ 6 - 1
vmcore/include/macro.h

@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <inttypes.h>
 #include <stdint.h>
+#include <time.h>
 
 #include <assert.h>
 #include <errno.h>
@@ -34,8 +35,12 @@
 
 #ifdef NDEBUG
 #define errasert(e) ((void)0)
+#elifdef __assert_fail
+#define errasert(e) __assert_fail(#e, __FILE__, __LINE__, __ASSERT_FUNCTION)
+#elifdef _assert
+#define errasert(e) _assert(#e, __FILE__, __LINE__)
 #else
-#define errasert(e) __assert_fail (#e, __FILE__, __LINE__, __ASSERT_FUNCTION)
+#define errasert(e) fprintf(stderr, "%s %s %s %s\n", #e, __FILE__, __LINE__)
 #endif
 
 #endif //VIRTUALMATH_MACRO_H

+ 1 - 1
vmcore/include/run.h

@@ -16,7 +16,7 @@ typedef struct DecorationStatement DecorationStatement;
 
 typedef ResultType (*VarInfo)(wchar_t **name, int *times, FUNC);
 
-ResultType globalIterStatement(Result *result, Inter *inter, Statement *st);
+ResultType globalIterStatement(Result *result, Inter *inter, Statement *st, bool p_clock);
 bool operationSafeInterStatement(FUNC);
 bool ifBranchSafeInterStatement(FUNC);
 bool functionSafeInterStatement(FUNC);

+ 7 - 1
vmcore/src/run.c

@@ -249,12 +249,13 @@ ResultType iterStatement(FUNC) {
  * @param inter
  * @return
  */
-ResultType globalIterStatement(Result *result, Inter *inter, Statement *st) {
+ResultType globalIterStatement(Result *result, Inter *inter, Statement *st, bool p_clock) {
     ResultType type;
     VarList *var_list = NULL;
     Statement *base;
     LinkValue *belong = inter->base_belong;
     void *bak = NULL;
+    clock_t start, stop;
 
     if (st == NULL){
         setResult(result, inter);
@@ -264,6 +265,8 @@ ResultType globalIterStatement(Result *result, Inter *inter, Statement *st) {
     is_KeyInterrupt = signal_reset;
     bak = signal(SIGINT, signalStopInter);
     gc_addTmpLink(&belong->gc_status);
+
+    start = clock();
     do {
         base = st;
         var_list = inter->var_list;
@@ -290,11 +293,14 @@ ResultType globalIterStatement(Result *result, Inter *inter, Statement *st) {
                 base = base->next;
         }
     } while (type == R_restart && result->times == 0);
+    stop = clock();
 
     if (type != R_error && type != R_func)
         setResultOperationNone(result, inter, belong);
     result->node = base;
 
+    if (p_clock)
+        printf("run times = %Lf sec\n", (double long)(stop - start) / CLOCKS_PER_SEC);
     gc_freeTmpLink(&belong->gc_status);
     signal(SIGINT, bak);
     return result->type;

+ 1 - 1
vmcore/src/runfile.c

@@ -154,7 +154,7 @@ ResultType importVMFileCore(Inter *import_inter, char *path, fline line, char *c
     if (!importRunParser(pm, line, code_file, run_st, CNEXT_NT))
         goto return_;
 
-    globalIterStatement(result, import_inter, run_st);
+    globalIterStatement(result, import_inter, run_st, false);
     if (!CHECK_RESULT(result))
         setResultError(E_BaseException, NULL, line, code_file, false, CNEXT_NT);
     else