Comments (3)
I'll assume that getprimes()
produces a bunch of rows, one column wide, holding an integer. I don't really see why you're going to the trouble of using a prepared query for this; just txn.exec()
would be easier than preparing the query first and then calling txn.exec_prepared()
. The simpler way can even be faster, under some circumstances.
The iterators you're passing there are iterators of rows, and you probably don't want to store those in your vector. What you want is the actual numbers that are inside them.
Is the loop per se really the problem that you're worried about? Are you trying to avoid the loop because you don't like having the loop in your code, or is your real concern the inefficiency of reallocating the vector's memory as it grows?
If it's the loop per se that you want to avoid (e.g. because you just don't like loops), then we can use a functional callback style with pqxx::result::for_each()
.
If you're worried about the repeated memory allocations, you can call std::vector::reserve()
at the beginning of the loop. That way you allocate the vector's memory just once. But apply Knuth's Law β see whether this really is an issue, because std::vector
was designed to minimise this overhead.
So here's a sketch with those two changes:
std::vector<int> Primes;
pqxx::result const r(txn.exec_prepared("getprimes"));
// Allocate all memory at once, now that we know how many primes we're getting.
Primes.reserve(r.size());
// Store query results in vector. This converts each row to an arguments list
// (in this case just the one field) and calls the lambda we pass.
r.for_each([&Primes](int prime) { Primes.push_back(prime); });
However if you're retrieving a lot of results, this may actually be faster using a streaming query. Those don't use pqxx::result
, which has the advantages that it can start delivering results before it's done executing the query, and that it tends to be faster overall for large result sets. It has the disadvantage that you won't know beforehand how many rows you're going to get.
Luckily std::vector::reserve()
works just fine even if you don't get the exact right number. You can just use a reasonable guess. For example, you can "reserve" a number that you think is a reasonable allocation to make, even if it's more memory than you actually need. In that case you're still doing only one allocation, just an oversized one. Or if it's too small, you'll do multiple allocations, but not as many as before:
std::vector<int> Primes;
// Just an acceptable guess at how many primes we'll get.
Primes.reserve(10'000'000);
// Execute "select getprimes()", and don't wait for the whole query
// to complete, but start processing rows right away. Convert each
// to a tuple of, in this case, just a single int.
for (auto [prime] : txn.stream<int>("select getprimes()"))
Primes.push_back(prime);
from libpqxx.
Hi Jeroen, to answer your question first
I don't really see why you're going to the trouble of using a prepared query for this
It's because I don't know libpqxx very well and thought there must be a cleaner way , I know there is always a loop of some sort going on but if I can avoid a visible one I will
Thank you for your excellent suggestions, I like the lambda method best but both are nice to know. Thanks again.
from libpqxx.
OK, I'll close the ticket then.
from libpqxx.
Related Issues (20)
- Compile error - error: invalid use of βthisβ at top level HOT 10
- vcpkg installation - link errors HOT 7
- Bind results of selects directly to a variable of type std::vector for array types HOT 9
- Efficient UUIDs HOT 3
- How to pass custom class to postgres function HOT 65
- PQXX_VERSION says '7.9.0' but I used tag 7.8.1 HOT 1
- User code won't compile with C++20 or C++23 HOT 4
- `splitconfig` should be `splitconfig.py`
- Transaction declaration error HOT 10
- This doesn't compile on Linux HOT 9
- Array types with other containers HOT 4
- Iterator performance HOT 12
- Streams as C++20 ranges HOT 5
- Stream fails parsing empty `varchar`/`text` fields HOT 4
- Update source question HOT 6
- vcpkg libpqxx crashes upon connection destruction HOT 16
- Is calling to exec_prepared() considered as blocking operation ? HOT 2
- Not an issue but a favour request HOT 1
- Is it possible to build libpqxx without SSL support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libpqxx.