<p>We will compare modern libraries with segwit and bech32 support.</p>

<p>First will be <a href="https://pybtc.readthedocs.io/" rel="nofollow noreferrer">pybtc</a> from bitaps.com, second will be <a href="https://github.com/chainside/btcpy" rel="nofollow noreferrer">btcpy</a> from chainside.net</p>

<p>Our test code:</p>

<pre><code>import time
import pybtc

f = open('raw_block.txt')
fc = f.readline()
qt = time.time()
block_hex = fc[:-1]
bt = pybtc.Block(block_hex, format="decoded")
print("pybtc library parsed block in decoded format", time.time() - qt )

qt = time.time()
bt = pybtc.Block(block_hex, format="raw")
print("pybtc library parsed block in raw format", time.time() - qt )

from btcpy.structs.block import Block as btcpy_block

qt = time.time()
block = btcpy_block.unhexlify(block_hex)
print("btcpy library parsed block", time.time() - qt )
</code></pre>

<p>Raw block that used you can download <a href="https://github.com/bitaps-com/pybtc/blob/master/pybtc/test/raw_block.txt" rel="nofollow noreferrer">here</a></p>

<p>Test results:</p>

<pre><code>pybtc library parsed block in decoded format 0.3380300998687744
pybtc library parsed block in raw format 0.0953378677368164
btcpy library parsed block 0.6518878936767578
</code></pre>

<p>You can see that pybtc block parse performance is better. Decoded mode means that all bytes converted to hex and addresses to base58 or bech32. Raw mode faster, but all objects in raw bytes, addresses stored as hashes.</p>

CC BY-SA 4.0
2019-02-08T12:13:16.480Z
2019-07-08T17:30:02.997Z
2019-07-08T17:30:02.997Z
bitaps.com
<p>Check this library <a href="https://github.com/bitaps-com/pybtc" rel="nofollow noreferrer">pybtc</a></p>

<p>Class to decode raw block 
<a href="https://github.com/bitaps-com/pybtc/blob/master/pybtc/block.py#L5" rel="nofollow noreferrer">https://github.com/bitaps-com/pybtc/blob/master/pybtc/block.py#L5</a></p>

CC BY-SA 4.0
2019-02-08T03:51:20.407Z
Link only answers are discouraged. Add some explanation, background, anything to make this not just promotion of your own service and API.
Claris
CC BY-SA 4.0
2019-02-08T11:15:47.433Z
This is not service or API this is open source library
bitaps.com
CC BY-SA 4.0
2019-08-07T21:13:05.507Z
Link-only answers are still discouraged.
Pieter Wuille
CC BY-SA 4.0
2023-03-23T17:34:26.910Z
Second link is now a 404 - which demonstrates the reason for discouraging link-only answers.
RedGrittyBrick
CC BY-SA 4.0
2019-02-08T03:04:28.460Z
2019-02-08T03:04:28.460Z
bitaps.com
<p>Let's say one is connected to a zmq socket and listening for rawblock. How you'd parse the block header is as follows:</p>

<pre><code>block_header = raw[:80]
version = block_header[:4]
prev_merkle_root = block_header[4:36]
merkle_root = block_header[36:68]
timestamp = block_header[68:72]
bits = block_header[72:76]
nonce = blockheader[76:80]
</code></pre>

<p>But, what is the best way to parse all the transactions in the block? Converting a rawtx to a tx object is easy, plenty of libraries do it. But what is the best way to parse a variable length block of txs?</p>

<transactions><block><block-header>
What is the most optimal way to parse a raw bitcoin block with python?
arshbot
# What is the most optimal way to parse a raw bitcoin block with python?

(Asked by: username_0 on 2019-02-07)

Let's say one is connected to a zmq socket and listening for rawblock. How you'd parse the block header is as follows:

```
block_header = raw[:80]
version = block_header[:4]
prev_merkle_root = block_header[4:36]
merkle_root = block_header[36:68]
timestamp = block_header[68:72]
bits = block_header[72:76]
nonce = blockheader[76:80]

```

But, what is the best way to parse all the transactions in the block? Converting a rawtx to a tx object is easy, plenty of libraries do it. But what is the best way to parse a variable length block of txs?

**Tags:** transactions, block, block-header


---

## 2 answers

### Answer by username_1 on 2019-02-08. Score: 3

We will compare modern libraries with segwit and bech32 support.

First will be [pybtc](https://pybtc.readthedocs.io/) from username_1, second will be [btcpy](https://github.com/chainside/btcpy) from chainside.net

Our test code:

```
import time
import pybtc

f = open('raw_block.txt')
fc = f.readline()
qt = time.time()
block_hex = fc[:-1]
bt = pybtc.Block(block_hex, format="decoded")
print("pybtc library parsed block in decoded format", time.time() - qt )

qt = time.time()
bt = pybtc.Block(block_hex, format="raw")
print("pybtc library parsed block in raw format", time.time() - qt )

from btcpy.structs.block import Block as btcpy_block

qt = time.time()
block = btcpy_block.unhexlify(block_hex)
print("btcpy library parsed block", time.time() - qt )

```

Raw block that used you can download [here](https://github.com/username_1/pybtc/blob/master/pybtc/test/raw_block.txt)

Test results:

```
pybtc library parsed block in decoded format 0.3380300998687744
pybtc library parsed block in raw format 0.0953378677368164
btcpy library parsed block 0.6518878936767578

```

You can see that pybtc block parse performance is better. Decoded mode means that all bytes converted to hex and addresses to base58 or bech32. Raw mode faster, but all objects in raw bytes, addresses stored as hashes.


---

### Answer by username_1 on 2019-02-08. Score: -3

Check this library [pybtc](https://github.com/username_1/pybtc)

Class to decode raw block
<https://github.com/username_1/pybtc/blob/master/pybtc/block.py#L5>

> **Comments:**
> **username_2**: Link only answers are discouraged. Add some explanation, background, anything to make this not just promotion of your own service and API.  
> **username_1**: This is not service or API this is open source library  
> **username_3**: Link-only answers are still discouraged.  
> **username_4**: Second link is now a 404 - which demonstrates the reason for discouraging link-only answers.  

---

