Saving elements of a vector in ZPL is very slow!

  • 6 July 2021
  • 3 replies



I made a ZPL macro that uses GETPSF to retrieve a PSF to a vector and then saves the vector to a file with a FOR loop on the vector elements. The saving part is extremely low! Is there a better way to save the results?

I tried doing the same thing with GETTEXTFILE but the speed is similar, and I lose control on file format and can't seem to find a file to set the parameters such as sampling size.

Any help really appreciated!



3 replies

Userlevel 6
Badge +2

Hi Paolo

One thing that can make ZPL slow is the PRINT keyword. In the help file, it is written:

'Note that very large quantities of PRINT commands (i.e. thousands of lines or more) can negatively impact macro execution speed if the printed text is displayed on the screen (i.e. OUTPUT SCREEN).'

'The best solution is to use PRINT to save to a file instead of pushing to the window (i.e. use OUTPUT filename). It will make the macro more efficient, by avoiding the inefficiencies of printing to the ZPL output window.'

So I guess this is the 1st thing I would check. When you say very slow, how long does it take? What is the sampling of the PSF you are reading? Would you have an example we could try? Another way would be to work in ZOS-API.



Dear Sandrine,

thank you very much for your answer!

I am already printing directly on a file, not on screen. Here below is the bit of code I am using.The PSF computing part seems to be quite fast (I am using sampling=3): it's really the writing to disk that takes most of the time. Do you know of any way other than ZOS-API? I don't have a professional license and cannot get one soon enough.

    temp_file$ = $TEMPFILENAME()

    OUTPUT temp_file$


    FOR field= 1, n_fields, 1

        GETPSF wave, field, sampling, vector, unnormalized, phaseflag, imagedelta


        np = vec1(0)

        FORMAT .7 EXP

        FOR i= 1, np, 1

            print vec1(i)



Thank you!

Userlevel 6
Badge +2

Hi Paolo

I run a test on the sample file '\Zemax\Samples\Sequential\Objectives\Double Gauss 28 degree field.zmx' with the attached script. The sampling is set at 3. It runs in about 5 seconds (a bit less) on my computer. How long does it take on yours?

So one thing you could check is that you are saving on a local file, not a file that is on a shared folder. Sometimes the access time can be a bit longer. 

temp_file$ = 'C:\Data\PSF.txt'

Let me know if that helps or not.