square / gifencoder

A pure Java library implementing the GIF89a specification. Suitable for use on Android.
Apache License 2.0
664 stars 75 forks source link

addImage() very slow? #20

Closed Kasuyakema closed 4 years ago

Kasuyakema commented 4 years ago

Hey guys, first of thanks for sharing the code.

I tested this library in my project and experienced very slow encoding times. Testing diferent options this is the fastest time per 1080p frame i could reach:

Gif done in 180771ms 30 frames at avg 6025ms

the vast majority of that time is spend in the addImage() method.

Is this normal? am i doing something wrong?

Thanks in advance, Martin

dlubarov commented 4 years ago

Hi Martin,

We don't use global color tables or anything, so we quantize colors and encode images on a per-frame basis in addImage. So it makes sense that you're seeing that method as the bottleneck.

Our original use case involved pretty small images, so we focused on simple code at the expense of performance. I.e. it's all single-threaded, and each color is wrapped in a Color object.

UniformQuantizer should be the cheapest quantizer, so I would try setColorQuantizer(UniformQuantizer.INSTANCE) and see if that helps. That said, there's still a lot of unnecessary allocation/GC, double precision arithmetic, and a general lack of optimization work, so it might still be fairly slow!

Kasuyakema commented 4 years ago

Hey thanks for the fast reply,

i was using the UniformQuantizer in that test and still needed 6s per frame. I guess i just have the wrong use case for this lib. I was planning to encode 300+ Frames with up to 1080p. Im making a feature for my game to capture the last up to 30s of gameplay as a gif.

Thanks for the Help i will close this issue