Skip to content

Instantly share code, notes, and snippets.

@ramntry
Last active December 28, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramntry/7552002 to your computer and use it in GitHub Desktop.
Save ramntry/7552002 to your computer and use it in GitHub Desktop.
var arr = [];
for (var i = 0; i < 30000; i++) {
arr.push(Math.round(Math.random() * 1.0e9).toString());
}
Array.prototype.diff = function(a) {
return this.filter(function(i) { return !(a.indexOf(i) > -1); });
};
for (var i = 0; i < 20; ++i) {
console.log(arr.diff(arr).length == 0);
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static inline size_t calc_hash(char const *str)
{
size_t acc = 0;
while (*str) {
acc *= 223;
acc += *str++;
}
return acc;
}
static char **gen_array_of_random_strings(size_t size)
{
char **array = (char **)malloc(size * sizeof(char *));
for (size_t i = 0; i < size; ++i) {
array[i] = (char *)malloc(10);
sprintf(array[i], "%d", rand() % 1000000000);
}
return array;
}
static void print_array_of_strings(char **array, size_t size)
{
putchar('[');
if (size)
printf(" '%s'", array[0]);
for (size_t i = 1; i < size; ++i)
printf(",\n '%s'", array[i]);
printf(" ]\n");
}
static void free_array_of_strings(char **array, size_t size)
{
for (size_t i = 0; i < size; ++i)
free(array[i]);
free(array);
}
struct Bucket
{
char *str;
struct Bucket *next;
};
static struct Bucket *create_hash(size_t numof_buckets)
{
return (struct Bucket *)calloc(numof_buckets, sizeof(struct Bucket));
}
static inline void insert(struct Bucket *hash, size_t numof_buckets, char *str)
{
struct Bucket *bucket = &hash[calc_hash(str) % numof_buckets];
if (bucket->str) {
struct Bucket *new_bucket = (struct Bucket *)malloc(sizeof(struct Bucket));
new_bucket->str = str;
new_bucket->next = bucket->next;
bucket->next = new_bucket;
} else
bucket->str = str;
}
static inline int has(struct Bucket *hash, size_t numof_buckets, char *str)
{
struct Bucket *bucket = &hash[calc_hash(str) % numof_buckets];
if (bucket->str && strcmp(bucket->str, str) == 0)
return 1;
bucket = bucket->next;
while (bucket) {
if (strcmp(bucket->str, str) == 0)
return 1;
bucket = bucket->next;
}
return 0;
}
static void dump_hash(struct Bucket *hash, size_t numof_buckets)
{
for (size_t i = 0; i < numof_buckets; ++i) {
printf("[%5zu]: [ ", i);
struct Bucket *bucket = &hash[i];
if (bucket->str)
printf("%9s", bucket->str);
bucket = bucket->next;
while (bucket) {
printf(", %9s", bucket->str);
bucket = bucket->next;
}
printf(" ]\n");
}
}
static size_t max_load(struct Bucket *hash, size_t numof_buckets)
{
size_t acc = 0;
for (size_t i = 0; i < numof_buckets; ++i) {
size_t curr_load = 0;
struct Bucket *bucket = &hash[i];
if (bucket->str)
++curr_load;
bucket = bucket->next;
while (bucket) {
++curr_load;
bucket = bucket->next;
}
acc = curr_load > acc ? curr_load : acc;
}
return acc;
}
static void free_hash(struct Bucket *hash, size_t numof_buckets)
{
for (size_t i = 0; i < numof_buckets; ++i) {
struct Bucket *list = hash[i].next;
while (list) {
struct Bucket *to_free = list;
list = list->next;
free(to_free);
}
}
free(hash);
}
int hasAll(char **this, size_t this_size, char **a, size_t a_size)
{
size_t const numof_buckets = this_size * 2;
struct Bucket *hash = create_hash(numof_buckets);
for (size_t i = 0; i < this_size; ++i)
insert(hash, numof_buckets, this[i]);
int result = 1;
for (size_t i = 0; i < a_size; ++i)
if (!has(hash, numof_buckets, a[i])) {
result = 0;
break;
}
free_hash(hash, numof_buckets);
return result;
}
int main()
{
size_t const size = 30000;
size_t const numof_repetitions = 20;
char **array = gen_array_of_random_strings(size);
for (size_t i = 0; i < numof_repetitions; ++i)
printf("%s\n", hasAll(array, size, array, size) ? "true" : "false");
free_array_of_strings(array, size);
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static inline size_t calc_hash(char const *str)
{
size_t acc = 0;
while (*str) {
acc *= 223;
acc += *str++;
}
return acc;
}
static char **gen_array_of_random_strings(size_t size)
{
char **array = (char **)malloc(size * sizeof(char *));
for (size_t i = 0; i < size; ++i) {
array[i] = (char *)malloc(10);
sprintf(array[i], "%d", rand() % 1000000000);
}
return array;
}
static void print_array_of_strings(char **array, size_t size)
{
putchar('[');
if (size)
printf(" '%s'", array[0]);
for (size_t i = 1; i < size; ++i)
printf(",\n '%s'", array[i]);
printf(" ]\n");
}
static void free_array_of_strings(char **array, size_t size)
{
for (size_t i = 0; i < size; ++i)
free(array[i]);
free(array);
}
struct Bucket
{
char *str;
};
static struct Bucket *create_hash(size_t numof_buckets)
{
return (struct Bucket *)calloc(numof_buckets, sizeof(struct Bucket));
}
static inline void insert(struct Bucket *hash, size_t numof_buckets, char *str)
{
size_t pos = calc_hash(str) % numof_buckets;
while (hash[pos].str)
pos = (pos + 1) % numof_buckets;
hash[pos].str = str;
}
static inline int has(struct Bucket *hash, size_t numof_buckets, char *str)
{
size_t pos = calc_hash(str) % numof_buckets;
while (hash[pos].str) {
if (strcmp(hash[pos].str, str) == 0)
return 1;
pos = (pos + 1) % numof_buckets;
}
return 0;
}
static void dump_hash(struct Bucket *hash, size_t numof_buckets)
{
for (size_t i = 0; i < numof_buckets; ++i) {
printf("[%5zu]: [ ", i);
struct Bucket *bucket = &hash[i];
if (bucket->str)
printf("%9s", bucket->str);
printf(" ]\n");
}
}
static size_t max_load(struct Bucket *hash, size_t numof_buckets)
{
size_t acc = 0;
size_t contiguous_used = 0;
for (size_t i = 0; i < numof_buckets; ++i)
if (hash[i].str)
++contiguous_used;
else {
acc = contiguous_used > acc ? contiguous_used : acc;
contiguous_used = 0;
}
return acc;
}
static void free_hash(struct Bucket *hash, size_t numof_buckets)
{
free(hash);
}
int hasAll(char **this, size_t this_size, char **a, size_t a_size)
{
size_t const numof_buckets = this_size * 4;
struct Bucket *hash = create_hash(numof_buckets);
for (size_t i = 0; i < this_size; ++i)
insert(hash, numof_buckets, this[i]);
int result = 1;
for (size_t i = 0; i < a_size; ++i)
if (!has(hash, numof_buckets, a[i])) {
result = 0;
break;
}
free_hash(hash, numof_buckets);
return result;
}
int main()
{
size_t const size = 30000;
size_t const numof_repetitions = 20;
char **array = gen_array_of_random_strings(size);
for (size_t i = 0; i < numof_repetitions; ++i)
printf("%s\n", hasAll(array, size, array, size) ? "true" : "false");
free_array_of_strings(array, size);
}
var arr = [];
for (var i = 0; i < 30000; i++) {
arr.push(Math.round(Math.random() * 1.0e9).toString());
}
Array.prototype.hasAll = function(a) {
var hash = this.reduce(function(acc, i) { acc[i] = true; return acc; }, {});
return a.every(function(i) { return i in hash; });
};
for (var i = 0; i < 20; ++i) {
console.log(arr.hasAll(arr));
}
@ramntry
Copy link
Author

ramntry commented Nov 19, 2013

ramntry@ramntry-R418:~/studies/javascript/hasAll$ gcc hasAll.c -std=c99 -O3 -o hasAll
ramntry@ramntry-R418:~/studies/javascript/hasAll$ time ./hasAll 
true
-//-
true

real 0m0.180s
user 0m0.176s
sys  0m0.000s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ time ./hasAll2
true
-//-
true

real 0m0.156s
user 0m0.152s
sys  0m0.004s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ time nodejs ramntry.js 
true
-//-
true

real 0m0.737s
user 0m0.648s
sys  0m0.100s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ time nodejs egor_nullptr.js 
true
-//-true

real 2m59.548s
user 3m1.795s
sys  0m1.460s
ramntry@ramntry-R418:~/studies/javascript/hasAll$ nodejs --version
v0.10.22
ramntry@ramntry-R418:~/studies/javascript/hasAll$ gcc --version
gcc (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
ramntry@ramntry-R418:~/studies/javascript/hasAll$ cat /proc/cpuinfo
processor   : 0
-//-
model name  : Intel(R) Pentium(R) Dual  CPU  T3400  @ 2.16GHz
-//-
cache size  : 1024 KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment