From 133d5715a3ba4a26325ae57f9c2f07f3f591cbdd Mon Sep 17 00:00:00 2001 From: Domingo Alvarez Duarte Date: Wed, 11 Apr 2018 11:26:47 +0200 Subject: [PATCH 1/2] Move the call to strlen outside the for loop comparison It's not necessary to recalculate the "strlen(word)" on every loop comparison when "word" doesn't change inside the loop. --- word2vec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/word2vec.c b/word2vec.c index 9d6fffd..f7690ef 100644 --- a/word2vec.c +++ b/word2vec.c @@ -96,8 +96,8 @@ void ReadWord(char *word, FILE *fin, char *eof) { // Returns hash value of a word int GetWordHash(char *word) { - unsigned long long a, hash = 0; - for (a = 0; a < strlen(word); a++) hash = hash * 257 + word[a]; + unsigned long long a, str_len, hash = 0; + for (a = 0, str_len = strlen(word); a < str_len; a++) hash = hash * 257 + word[a]; hash = hash % vocab_hash_size; return hash; } From 9b658dec3a3590d087460a7ae9702db00b76df81 Mon Sep 17 00:00:00 2001 From: Domingo Alvarez Duarte Date: Wed, 11 Apr 2018 11:42:59 +0200 Subject: [PATCH 2/2] Move call to strlen outside for loop comparison Here also the same code as in word2vec.c --- word2phrase.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/word2phrase.c b/word2phrase.c index db58809..1ba6e19 100644 --- a/word2phrase.c +++ b/word2phrase.c @@ -69,8 +69,8 @@ void ReadWord(char *word, FILE *fin, char *eof) { // Returns hash value of a word int GetWordHash(char *word) { - unsigned long long a, hash = 1; - for (a = 0; a < strlen(word); a++) hash = hash * 257 + word[a]; + unsigned long long a, str_len, hash = 1; + for (a = 0, str_len = strlen(word); a < str_len; a++) hash = hash * 257 + word[a]; hash = hash % vocab_hash_size; return hash; }