Commit 16c32595 by songxinkai

fix c++python example bug: Py_DECREF

parent 23bd707d
File added
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream f;
f.open("out.txt", ios::app|ios::out);
f << "line1, " << 1;
f << ", " << 2;
f << "\n";
f << "line2, " << 1;
f << ", " << 2;
f << "\n";
return 0;
}
line1, 1, 2
line2, 1, 2
line1, 1, 2
line2, 1, 2
/*
Reference:
https://en.cppreference.com/w/cpp/header/iomanip
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main (){
cout << 12 << '\r';
cout << 2 << '\r';
cout << 3 << '\r' << endl;
int a = 12;
cout << "dec: " << dec << a << endl;
cout << "hex: " << hex << a << endl;
cout << "oct: " << setbase(10) << a << endl;
cout << "oct: " << setbase(3) << a << endl;
char *pt = "China";
cout << setw(10) << pt <<endl;
cout << setw(-10) << pt <<endl;
cout << setfill('*') << setw(10) << pt << endl;
cout << setfill('*') << setw(-10) << pt << endl;
double pi = 22.0 / 7.0;
cout << "pi = " << setprecision(2) << pi << endl;
cout << setiosflags(ios::scientific);
cout << "pi = " << setprecision(5) << pi << endl;
cout << resetiosflags(ios::scientific);
cout << "pi = " << setprecision(2) << pi << endl; // set precision
cout << "pi = " << setiosflags(ios::fixed) << setprecision(4) << pi << endl; // set precision behind point
return 0;
}
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
\ No newline at end of file
File added
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
\ No newline at end of file
...@@ -2,6 +2,7 @@ import numpy as np ...@@ -2,6 +2,7 @@ import numpy as np
def save_np(input_data, fn): def save_np(input_data, fn):
a = np.array(input_data) a = np.array(input_data)
print a
a.tofile(fn) a.tofile(fn)
def load_np(fn): def load_np(fn):
......
import numpy as np
def show_array(a, b):
print np.array(a)
print np.array(b)
...@@ -39,6 +39,7 @@ void py_save_load(int thread_id){ ...@@ -39,6 +39,7 @@ void py_save_load(int thread_id){
PyTuple_SetItem(pArgs, 0, Py_BuildValue("s",s.c_str())); PyTuple_SetItem(pArgs, 0, Py_BuildValue("s",s.c_str()));
PyObject_CallObject(pFunc, pArgs); PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pName); Py_DECREF(pName);
Py_DECREF(tuple_0);
Py_DECREF(pArgs); Py_DECREF(pArgs);
Py_DECREF(pModule); Py_DECREF(pModule);
mtx.unlock(); mtx.unlock();
......
/**g++ -o callpy callpy.cpp -I/usr/include/python2.7 -L/usr/lib64/python2.7/config -lpython2.7**/
#include <Python.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <time.h>
#include <unistd.h>
using namespace std;
void py_save_load(){
int j = 0;
Py_Initialize() ;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject *pName,*pModule,*pDict,*pFunc;
pName = PyString_FromString("show_ndarray");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, "show_array");
while(true){
int len = 1024*1024;
PyObject* pArgs = PyTuple_New(2);
PyObject* tuple_0 = PyTuple_New(len);
PyObject* list_0 = PyList_New(0);
for (int i = 0; i < len; ++i){
PyTuple_SetItem(tuple_0, i, Py_BuildValue("f",0.1*i));
PyList_Append(list_0, Py_BuildValue("d",i));
}
PyTuple_SetItem(pArgs, 0, tuple_0);
PyTuple_SetItem(pArgs, 1, list_0);
PyObject_CallObject(pFunc, pArgs);
Py_DECREF(tuple_0);
Py_DECREF(list_0);
Py_DECREF(pArgs);
cout << j++ << endl;
sleep(1);
}
Py_DECREF(pName);
Py_DECREF(pModule);
}
int main(int argc, char** argv){
py_save_load();
cout << "=============" << endl;
return 0;
}
No preview for this file type
...@@ -51,14 +51,20 @@ vector<string> string_split(const string &s, const string &seperator){ ...@@ -51,14 +51,20 @@ vector<string> string_split(const string &s, const string &seperator){
} }
int main(){ int main(){
int i1 = 199, i21, i22; //int i1 = 199, i21, i22;
string s1, s2="288_377", s3 = "finish"; //string s1, s2="288_377", s3 = "finish";
i2s(i1, s1); //i2s(i1, s1);
vector<string> vec_s = string_split(s2, "_"); //vector<string> vec_s = string_split(s2, "_");
s2i(vec_s[0], i21); //s2i(vec_s[0], i21);
s2i(vec_s[1], i22); //s2i(vec_s[1], i22);
cout << ("finish" == s3) << ", " << ("288" == s3) << endl; //cout << ("finish" == s3) << ", " << ("288" == s3) << endl;
cout << i1 << ", " << s1 << endl; //cout << i1 << ", " << s1 << endl;
cout << s2 << ", " << i21 << " _ " << i22 << endl; //cout << s2 << ", " << i21 << " _ " << i22 << endl;
vector<string> vec_s_0 = string_split("model-10000.uff", "model-");
vector<string> vec_s_1 = string_split(vec_s_0[0], ".uff");
int i;
s2i(vec_s_1[0], i);
cout << i << endl;
return 0; return 0;
} }
No preview for this file type
// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
using std::chrono::system_clock;
using std::chrono::high_resolution_clock;
int main ()
{
std::chrono::duration<int,std::ratio<60*60*24> > one_day (1);
std::chrono::duration<int,std::ratio<1> > one_sec (1);
system_clock::time_point today = system_clock::now();
system_clock::time_point tomorrow = today + one_day;
std::time_t tt;
tt = system_clock::to_time_t ( today );
std::cout << "today is: " << ctime(&tt);
tt = system_clock::to_time_t ( tomorrow );
std::cout << "tomorrow will be: " << ctime(&tt);
return 0;
}
// high_resolution_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
int main ()
{
using namespace std::chrono;
high_resolution_clock::time_point t1 = high_resolution_clock::now();
std::cout << "printing out 1000 stars...\n";
for (int i=0; i<1000; ++i) std::cout << "*";
std::cout << std::endl;
high_resolution_clock::time_point t2 = high_resolution_clock::now();
std::cout<<"in seconds time:";
duration<double,std::ratio<1,1>> duration_s(t2-t1);
std::cout<<duration_s.count()<<" seconds"<<std::endl;
std::cout<<"in millisecond time:";
//duration<double,std::ratio<1,1000>> duration_ms(t2-t1);
duration<double,std::ratio<1,1000>> duration_ms=duration_cast<duration<double,std::ratio<1,1000>>>(t2-t1);
std::cout<<duration_ms.count()<<" milliseconds"<<std::endl;
std::cout<<"in microsecond time:";
//duration<double,std::ratio<1,1000000>> duration_ms(t2-t1);
duration<double,std::ratio<1,1000000>> duration_mcs=duration_cast<duration<double,std::ratio<1,1000000>>>(t2-t1);
std::cout<<duration_mcs.count()<<" microseconds"<<std::endl;
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took me " << time_span.count() << " seconds.";
std::cout << std::endl;
return 0;
}
...@@ -7,9 +7,6 @@ using namespace std; ...@@ -7,9 +7,6 @@ using namespace std;
int main(){ int main(){
time_t a = 0, b; time_t a = 0, b;
if (a == 0){
cout << "xx" << endl;
}
time(&a); // time.h time(&a); // time.h
sleep(2); // unistd.h sleep(2); // unistd.h
time(&b); time(&b);
......
/*
* Tencent is pleased to support the open source community by making PhoenixGo available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "timer.h"
using namespace std::chrono;
Timer::Timer()
: m_start(clock::now())
{
}
void Timer::Reset()
{
m_start = clock::now();
}
int64_t Timer::sec() const
{
return duration_cast<seconds>(clock::now() - m_start).count();
}
int64_t Timer::ms() const
{
return duration_cast<milliseconds>(clock::now() - m_start).count();
}
int64_t Timer::us() const
{
return duration_cast<microseconds>(clock::now() - m_start).count();
}
float Timer::fsec() const
{
return std::chrono::duration<float>(clock::now() - m_start).count();
}
float Timer::fms() const
{
return std::chrono::duration<float, std::milli>(clock::now() - m_start).count();
}
float Timer::fus() const
{
return std::chrono::duration<float, std::micro>(clock::now() - m_start).count();
}
/*
* Tencent is pleased to support the open source community by making PhoenixGo available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <chrono>
class Timer
{
public:
Timer();
void Reset();
int64_t sec() const;
int64_t ms() const;
int64_t us() const;
float fsec() const;
float fms() const;
float fus() const;
private:
typedef std::chrono::system_clock clock;
clock::time_point m_start;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment