Skip to content

Instantly share code, notes, and snippets.

@nyanshell
Created March 17, 2014 08:49
Show Gist options
  • Save nyanshell/9595925 to your computer and use it in GitHub Desktop.
Save nyanshell/9595925 to your computer and use it in GitHub Desktop.
A test: using google sparse hash in python script
dict()
for i in range(0, 10000000):
d[str(i)] = i + 1
/*
compile:
g++ -c -fPIC -std=c++0x -O3 test_ctypes.cpp -o foo.o
g++ -shared -std=c++0x -Wl,-soname,libfoo.so -O3 -o libfoo.so foo.o
*/
#include <iostream>
#include <iostream>
#include <functional>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <sparsehash/sparse_hash_map>
#include <sparsehash/dense_hash_map>
using google::dense_hash_map; // namespace where class lives by default
using google::sparse_hash_map;
using std::cout;
using std::endl;
using std::hash;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return (s1 == s2) || (s1 && s2 && strcmp(s1, s2) == 0);
}
};
class Foo{
public:
dense_hash_map<char*, int, hash<const char*>, eqstr> h;
char* sm;
Foo()
{
h = dense_hash_map<char*, int, hash<const char*>, eqstr>(10000000);
//cout << "hello" << endl;
sm = new char[1];
memcpy(sm, "a", sizeof(new char[1]));
h.set_empty_key(NULL);
//cout << "done" << endl;
//memcpy(sm, "a", sizeof(new char[1]));
}
void insert(const char* key, const int value)
{
//cout << key << " " << value << endl;
//dense_hash_map<char*, int, hash<const char*>, eqstr> h(10000000);
char* cc = new char[strlen(key)];
memcpy(cc, key, sizeof(cc));
decltype(std::make_pair(cc, 0)) v = std::make_pair(cc, value);
h.insert(v);
return ;
}
int size()
{ return h.size(); }
/*
void test()
{
dense_hash_map<char*, int, hash<const char*>, eqstr> h(10000000);
h.set_empty_key(NULL);
char* sm = new char[1];
memcpy(sm, "a", sizeof(new char[1]));
for (int i = 1 ; i <= 10000000 ; ++ i )
{
std::string s = std::to_string(i);
char* cc = new char[s.size()];
memcpy(cc, s.c_str(), sizeof(cc));
decltype(std::make_pair(sm, 0)) v = std::make_pair(cc, i + 1);
h.insert(v);
}
cout << h.size() << endl;
}
void test2()
{
sparse_hash_map<char*, int, hash<const char*>, eqstr> h(10000000);
char* sm = new char[1];
memcpy(sm, "a", sizeof(new char[1]));
for (int i = 1 ; i <= 10000000 ; ++ i )
{
std::string s = std::to_string(i);
char* cc = new char[s.size()];
memcpy(cc, s.c_str(), sizeof(cc));
//cout << cc << " "<< typeid(cc).name() << " " << strcmp(cc, a[i-1]) << endl;
decltype(std::make_pair(sm, 0)) v = std::make_pair(cc, i + 1);
h.insert(v);
}
cout << h.size() << endl;
}
*/
};
extern "C" {
Foo* Foo_new(){ return new Foo(); }
//void Foo_bar(Foo* foo){ foo->bar(); }
//void Foo_test(Foo* foo){ foo->test(); }
//void Foo_test2(Foo* foo){ foo->test2(); }
void Foo_insert(Foo* foo, const char* k, int v){ foo->insert(k, v); }
int Foo_size(Foo* foo){ return foo->size(); }
}
from memory_profiler import profile
from ctypes import cdll
lib = cdll.LoadLibrary('./libfoo.so')
class Foo(object):
def __init__(self):
self.obj = lib.Foo_new()
"""
def bar(self):
lib.Foo_bar(self.obj)
def test(self):
lib.Foo_test(self.obj)
def test2(self):
lib.Foo_test2(self.obj)
def test0(self, x):
lib.Foo_test0(self.obj, x)
"""
def insert(self, k, v):
lib.Foo_insert(self.obj, k, v)
def size(self):
return lib.Foo_size(self.obj)
@profile
def main():
f = Foo()
#f.insert("adfadsf", 233);
for i in range(0, 10000000):
f.insert(str(i), i) #and you will see "Hello" on the screen
print f.size()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment