merge test case pybuf and benchmark pybuf_benchmark into a single test case
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10884 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
25f6cebe46
commit
32b01d9559
4 changed files with 68 additions and 58 deletions
|
|
@ -1,6 +1,7 @@
|
|||
%module pybuf
|
||||
%include<pybuffer.i>
|
||||
|
||||
%include<cstring.i>
|
||||
/*functions for the test case*/
|
||||
%pybuffer_mutable_binary(char *buf1, int len);
|
||||
%pybuffer_mutable_string(char *buf2);
|
||||
%pybuffer_binary(const char *buf3, int len);
|
||||
|
|
@ -32,3 +33,32 @@
|
|||
return strlen(buf4);
|
||||
}
|
||||
%}
|
||||
|
||||
/*functions for the benchmark*/
|
||||
%pybuffer_mutable_string(char *str1);
|
||||
%cstring_mutable(char *str2);
|
||||
|
||||
%inline %{
|
||||
void title(char *str) {
|
||||
int outword = 1;
|
||||
while(*str) {
|
||||
if (isalnum(*str)) {
|
||||
if (outword) {
|
||||
outword = 0;
|
||||
*str = toupper(*str);
|
||||
}
|
||||
}
|
||||
else {
|
||||
outword = 0;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
void title1(char *str1) {
|
||||
title(str1);
|
||||
}
|
||||
void title2(char *str2) {
|
||||
title(str2);
|
||||
}
|
||||
%}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
%module pybuf_benchmark
|
||||
|
||||
%include<pybuffer.i>
|
||||
%include<cstring.i>
|
||||
%pybuffer_mutable_string(char *str1);
|
||||
%cstring_mutable(char *str2);
|
||||
|
||||
%inline %{
|
||||
void title(char *str) {
|
||||
int outword = 0;
|
||||
while(*str) {
|
||||
if (isalnum(*str)) {
|
||||
if (outword) {
|
||||
outword = 1;
|
||||
*str = toupper(*str);
|
||||
}
|
||||
}
|
||||
else {
|
||||
outword = 0;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
void title1(char *str1) {
|
||||
title(str1);
|
||||
}
|
||||
void title2(char *str2) {
|
||||
title(str2);
|
||||
}
|
||||
%}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import pybuf
|
||||
import time
|
||||
k=1000000
|
||||
n=7
|
||||
|
||||
t=time.time()
|
||||
a = bytearray(b'hello world')
|
||||
for i in range(k):
|
||||
pybuf.title1(a)
|
||||
print "Time used by bytearray:",time.time()-t
|
||||
|
||||
t=time.time()
|
||||
b = 'hello world'
|
||||
for i in range(k):
|
||||
pybuf.title2(b)
|
||||
print "Time used by string:",time.time()-t
|
||||
|
|
@ -1,15 +1,42 @@
|
|||
#run:
|
||||
# python pybuf_runme3.py benchmark
|
||||
#for the benchmark, other wise the test case will be run
|
||||
import pybuf
|
||||
buf1 = bytearray(10)
|
||||
buf2 = bytearray(50)
|
||||
import sys
|
||||
if len(sys.argv)>=2 and sys.argv[1]=="benchmark":
|
||||
#run the benchmark
|
||||
import time
|
||||
k=1000000 #number of times to excute the functions
|
||||
|
||||
pybuf.func1(buf1)
|
||||
assert buf1 == b'a'*10
|
||||
t=time.time()
|
||||
a = bytearray(b'hello world')
|
||||
for i in range(k):
|
||||
pybuf.title1(a)
|
||||
print("Time used by bytearray:",time.time()-t)
|
||||
|
||||
pybuf.func2(buf2)
|
||||
assert buf2.startswith(b"Hello world!\x00")
|
||||
t=time.time()
|
||||
b = 'hello world'
|
||||
for i in range(k):
|
||||
pybuf.title2(b)
|
||||
print("Time used by string:",time.time()-t)
|
||||
else:
|
||||
#run the test case
|
||||
buf1 = bytearray(10)
|
||||
buf2 = bytearray(50)
|
||||
|
||||
count = pybuf.func3(buf2)
|
||||
assert count==10 #number of alpha and number in 'Hello world!'
|
||||
pybuf.func1(buf1)
|
||||
assert buf1 == b'a'*10
|
||||
|
||||
length = pybuf.func4(buf2)
|
||||
assert length==12
|
||||
pybuf.func2(buf2)
|
||||
assert buf2.startswith(b"Hello world!\x00")
|
||||
|
||||
count = pybuf.func3(buf2)
|
||||
assert count==10 #number of alpha and number in 'Hello world!'
|
||||
|
||||
length = pybuf.func4(buf2)
|
||||
assert length==12
|
||||
|
||||
buf3 = bytearray(b"hello")
|
||||
pybuf.title1(buf3)
|
||||
assert buf3==b'Hello'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue