Skip to content

Commit 13559c6

Browse files
committed
perf: improve random seed
Signed-off-by: Jianhui Zhao <[email protected]>
1 parent 5b805c7 commit 13559c6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (NOT LUA53_INCLUDE_DIRS OR NOT LUA53_LIBRARIES)
2626
message(FATAL_ERROR "Liblua 5.3 is required.")
2727
endif()
2828

29-
add_compile_options(-D_GNU_SOURCE -Os -Wall -Werror --std=gnu99 -fno-strict-aliasing)
29+
add_compile_options(-D_GNU_SOURCE -DLUA_USE_LINUX -Os -Wall -Werror --std=gnu99 -fno-strict-aliasing)
3030

3131
# configure a header file to pass some of the CMake settings to the source code
3232
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

eco.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Author: Jianhui Zhao <[email protected]>
44
*/
55

6+
#include <sys/time.h>
67
#include <stdlib.h>
78
#include <lualib.h>
89
#include <unistd.h>
@@ -681,6 +682,14 @@ static void show_usage(const char *progname)
681682
, progname);
682683
}
683684

685+
static void set_random_seed()
686+
{
687+
struct timeval t;
688+
689+
gettimeofday(&t, NULL);
690+
srandom(t.tv_usec * t.tv_sec);
691+
}
692+
684693
int main(int argc, char *const argv[])
685694
{
686695
struct ev_loop *loop = EV_DEFAULT;
@@ -691,13 +700,12 @@ int main(int argc, char *const argv[])
691700

692701
signal(SIGPIPE, SIG_IGN);
693702

703+
set_random_seed();
704+
694705
L = luaL_newstate();
695706

696707
luaL_openlibs(L);
697708

698-
luaL_loadstring(L, "math.randomseed(os.time())");
699-
lua_pcall(L, 0, 0, 0);
700-
701709
luaL_loadstring(L,
702710
"table.keys = function(t)"
703711
"local keys = {}"

0 commit comments

Comments
 (0)