time.c (1541B)
1 /* zlib License 2 * 3 * Copyright (c) 2017-2020 Ed van Bruggen 4 * 5 * This software is provided 'as-is', without any express or implied 6 * warranty. In no event will the authors be held liable for any damages 7 * arising from the use of this software. 8 * 9 * Permission is granted to anyone to use this software for any purpose, 10 * including commercial applications, and to alter it and redistribute it 11 * freely, subject to the following restrictions: 12 * 13 * 1. The origin of this software must not be misrepresented; you must not 14 * claim that you wrote the original software. If you use this software 15 * in a product, an acknowledgment in the product documentation would be 16 * appreciated but is not required. 17 * 2. Altered source versions must be plainly marked as such, and must not be 18 * misrepresented as being the original software. 19 * 3. This notice may not be removed or altered from any source distribution. 20 */ 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <time.h> 24 25 #include "../tisp.h" 26 27 /* return number of seconds since 1970 (unix time stamp) */ 28 static Val 29 prim_time(Tsp st, Hash env, Val args) 30 { 31 tsp_arg_num(args, "time", 0); 32 return mk_int(time(NULL)); 33 } 34 35 /* return time taken to run command given */ 36 static Val 37 prim_timeit(Tsp st, Hash env, Val args) 38 { 39 Val v; 40 clock_t t; 41 tsp_arg_num(args, "timeit", 1); 42 t = clock(); 43 if (!(v = tisp_eval(st, env, car(args)))) 44 return NULL; 45 t = clock() - t; 46 return mk_dec(((double)t)/CLOCKS_PER_SEC); 47 } 48 49 void 50 tib_env_time(Tsp st) 51 { 52 tsp_env_fn(time); 53 tsp_env_fn(timeit); 54 }