F
F
fogersp2014-11-19 11:14:27
PHP
fogersp, 2014-11-19 11:14:27

YAML: Python vs PHP?

Good afternoon. There is a yaml file. In php use yaml_parse_file to convert it to an array. Well, it seems to work well. Then, for the sake of interest, I decided to see how it looks in Python. Installed PyYAML .

import yaml
stream = open("example.yaml", 'r')
print yaml.load(stream)

I'm not strong in Python yet. For some reason, the output of the array in time is about 40 seconds, of which 20 seconds are just silence, and then the data output begins, while in PHP the output of print_r (array) or the array of the same file parsed by foreach takes 10 seconds. Why is there such a difference? Maybe another library? Or is the piece of code wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-11-19
@fogersp

yaml_parse_file is implemented in C, that is, there is nothing faster. But PyYAML is written in python. Try to parse in php using the symfony/yaml package - there will be approximately the same result (only disable the Peklov yaml module before that).

A
Anatoly Scherbakov, 2014-11-19
@Altaisoft

PyYAML also seems to have C modules. There is an example here: pyyaml.org/wiki/PyYAMLDocumentation
I think in your case it will look like this:

from yaml import load, dump

try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

with open('example.yaml', 'r') as stream:
    data = load(stream, Loader=Loader)

output = dump(data, Dumper=Dumper)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question