मैं भाषा के बारे में और जानने के लिए क्लोजर में एक साधारण डेस्कटॉप सर्च इंजन लिख रहा हूं। अब तक, मेरे कार्यक्रम के पाठ प्रसंस्करण चरण के दौरान प्रदर्शन वास्तव में बुरा है।क्लोजर में टेक्स्ट प्रसंस्करण प्रदर्शन में सुधार कैसे करें?
पाठ प्रसंस्करण मैं करने के लिए है के दौरान:
- साफ अप अवांछित पात्रों;
- स्ट्रिंग को लोअरकेस में कनवर्ट करें;
- शब्दों की सूची प्राप्त करने के लिए दस्तावेज़ को विभाजित करें;
- एक नक्शा बनाएं जो प्रत्येक शब्द को दस्तावेज़ में अपनी घटनाओं से जोड़ता है।
यहाँ कोड है: जैसा कि मैंने हास्केल में इस समस्या का another implementation है
(ns txt-processing.core
(:require [clojure.java.io :as cjio])
(:require [clojure.string :as cjstr])
(:gen-class))
(defn all-files [path]
(let [entries (file-seq (cjio/file path))]
(filter (memfn isFile) entries)))
(def char-val
(let [value #(Character/getNumericValue %)]
{:a (value \a) :z (value \z)
:A (value \A) :Z (value \Z)
:0 (value \0) :9 (value \9)}))
(defn is-ascii-alpha-num [c]
(let [n (Character/getNumericValue c)]
(or (and (>= n (char-val :a)) (<= n (char-val :z)))
(and (>= n (char-val :A)) (<= n (char-val :Z)))
(and (>= n (char-val :0)) (<= n (char-val :9))))))
(defn is-valid [c]
(or (is-ascii-alpha-num c)
(Character/isSpaceChar c)
(.equals (str \newline) (str c))))
(defn lower-and-replace [c]
(if (.equals (str \newline) (str c)) \space (Character/toLowerCase c)))
(defn tokenize [content]
(let [filtered (filter is-valid content)
lowered (map lower-and-replace filtered)]
(cjstr/split (apply str lowered) #"\s+")))
(defn process-content [content]
(let [words (tokenize content)]
(loop [ws words i 0 hmap (hash-map)]
(if (empty? ws)
hmap
(recur (rest ws) (+ i 1) (update-in hmap [(first ws)] #(conj % i)))))))
(defn -main [& args]
(doseq [file (all-files (first args))]
(let [content (slurp file)
oc-list (process-content content)]
(println "File:" (.getPath file)
"| Words to be indexed:" (count oc-list)))))
, मैं दोनों की तुलना में आप निम्नलिखित आउटपुट में देख सकते हैं।
Clojure संस्करण:
$ lein uberjar
Compiling txt-processing.core
Created /home/luisgabriel/projects/txt-processing/clojure/target/txt-processing-0.1.0-SNAPSHOT.jar
Including txt-processing-0.1.0-SNAPSHOT.jar
Including clojure-1.5.1.jar
Created /home/luisgabriel/projects/txt-processing/clojure/target/txt-processing-0.1.0-SNAPSHOT-standalone.jar
$ time java -jar target/txt-processing-0.1.0-SNAPSHOT-standalone.jar ../data
File: ../data/The.Rat.Racket.by.David.Henry.Keller.txt | Words to be indexed: 2033
File: ../data/Beyond.Pandora.by.Robert.J.Martin.txt | Words to be indexed: 1028
File: ../data/Bat.Wing.by.Sax.Rohmer.txt | Words to be indexed: 7562
File: ../data/Operation.Outer.Space.by.Murray.Leinster.txt | Words to be indexed: 7754
File: ../data/The.Reign.of.Mary.Tudor.by.James.Anthony.Froude.txt | Words to be indexed: 15418
File: ../data/.directory | Words to be indexed: 3
File: ../data/Home.Life.in.Colonial.Days.by.Alice.Morse.Earle.txt | Words to be indexed: 12191
File: ../data/The.Dark.Door.by.Alan.Edward.Nourse.txt | Words to be indexed: 2378
File: ../data/Storm.Over.Warlock.by.Andre.Norton.txt | Words to be indexed: 7451
File: ../data/A.Brief.History.of.the.United.States.by.John.Bach.McMaster.txt | Words to be indexed: 11049
File: ../data/The.Jesuits.in.North.America.in.the.Seventeenth.Century.by.Francis.Parkman.txt | Words to be indexed: 14721
File: ../data/Queen.Victoria.by.Lytton.Strachey.txt | Words to be indexed: 10494
File: ../data/Crime.and.Punishment.by.Fyodor.Dostoyevsky.txt | Words to be indexed: 10642
real 2m2.164s
user 2m3.868s
sys 0m0.978s
हास्केल संस्करण:
$ ghc -rtsopts --make txt-processing.hs
[1 of 1] Compiling Main (txt-processing.hs, txt-processing.o)
Linking txt-processing ...
$ time ./txt-processing ../data/ +RTS -K12m
File: ../data/The.Rat.Racket.by.David.Henry.Keller.txt | Words to be indexed: 2033
File: ../data/Beyond.Pandora.by.Robert.J.Martin.txt | Words to be indexed: 1028
File: ../data/Bat.Wing.by.Sax.Rohmer.txt | Words to be indexed: 7562
File: ../data/Operation.Outer.Space.by.Murray.Leinster.txt | Words to be indexed: 7754
File: ../data/The.Reign.of.Mary.Tudor.by.James.Anthony.Froude.txt | Words to be indexed: 15418
File: ../data/.directory | Words to be indexed: 3
File: ../data/Home.Life.in.Colonial.Days.by.Alice.Morse.Earle.txt | Words to be indexed: 12191
File: ../data/The.Dark.Door.by.Alan.Edward.Nourse.txt | Words to be indexed: 2378
File: ../data/Storm.Over.Warlock.by.Andre.Norton.txt | Words to be indexed: 7451
File: ../data/A.Brief.History.of.the.United.States.by.John.Bach.McMaster.txt | Words to be indexed: 11049
File: ../data/The.Jesuits.in.North.America.in.the.Seventeenth.Century.by.Francis.Parkman.txt | Words to be indexed: 14721
File: ../data/Queen.Victoria.by.Lytton.Strachey.txt | Words to be indexed: 10494
File: ../data/Crime.and.Punishment.by.Fyodor.Dostoyevsky.txt | Words to be indexed: 10642
real 0m9.086s
user 0m8.591s
sys 0m0.463s
मुझे लगता है कि (स्ट्रिंग ->आलसी अनुक्रम) Clojure कार्यान्वयन में रूपांतरण प्रदर्शन मार रहा है। मैं इसे कैसे सुधार सकता हूँ?
पीएस: इन परीक्षणों में उपयोग किए गए सभी कोड और डेटा को here डाउनलोड किया जा सकता है। इसके बजाय वर्णों के बीच अपने chars
char-val
के लिए, बस प्रत्यक्ष मूल्य की तुलना करना मानचित्रण के
1):
क्या जार के लिए जेवीएम स्टार्टअप समय है? क्या हास्केल के समान उपर है? – georgek
मेरी मशीन में JVM स्टार्टअप समय एक दूसरे के आसपास है। मुझे लगता है कि हास्केल क्रम प्रणाली (आरटीएस) की वजह से कुछ भूमि के ऊपर है, लेकिन मैं JVM तुलना में काफी कम किया जाना चाहिए। – luisgabriel
एस/मैं चाहिए/यह होना चाहिए/ – luisgabriel