what can help as well is putting the code into a function, so that the 
variables become local instead of global, which gives the compiler more 
optimisation opportunities, like so (I also changed some stylistic things :D):
    
    
    import streams, parsexml, times
    
    proc main() =
      const filename = "SwissProt.xml"
      
      let s = newFileStream(filename, fmRead)
      if s == nil: quit("cannot open the file " & filename)
      
      var
        attrkey, attrval, elemstart, data, line: string
        x: XmlParser
      
      let
        time = cpuTime()
        f = open("nim_output.csv", fmWrite)
      
      open(x, s, filename)
      while true:
        #walk through XML
        case x.kind
        of xmlAttribute:
          attrkey = x.attrKey
          if attrkey == "id":
            attrval = x.attrValue
        
        of xmlElementStart:
          elemstart = x.elementName
        
        of xmlCharData:
          data = x.charData
          if elemstart == "Species":
            line = attrval & ";" & elemstart & ";" & data
            f.writeLine(line)
        of xmlEof: break # end of file reached
        else: discard # ignore other events
        x.next()
      echo "Time taken: ", cpuTime() - time
      x.close()
    main()
    
    
    Run

Reply via email to