Make the program run faster

how to make a program run faster windows 10
how to make code run faster
how to make computer faster
how to speed up computer windows 7
programs take a long time to open windows 7
how to increase computer speed windows 10
fast pc
how to make a python program run faster

I have written a program which checks curse word in a text document. I convert the document into a list of words and pass each word to the site for checking if it is a curse word or not. Problem is if the text is too big, it is running very slow. How do I make it faster?

import urllib.request

def read_text():
   quotes = open(r"C:\Self\General\Pooja\Edu_Career\Learning\Python\Code\Udacity_prog_foundn_python\movie_quotes.txt") #built in function
   contents_of_file = quotes.read().split()
   #print(contents_of_file)
    quotes.close()
    check_profanity(contents_of_file)

def check_profanity(text_to_check):
   flag = 0
   for word in text_to_check:
   connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+word)
   output = connection.read()
   # print(output)
   if b"true" in output:     # file is opened in bytes mode and output is in byte so compare byte to byte
       flag= flag +1

   if flag > 0:
       print("profanity alert")
   else:
       print("the text has no curse words")

  connection.close()

read_text()

The website you are using supports more than one word per fetch. Hence, to make your code faster: A) Break the loop when you find the first curse word. B) Send super word to site. Hence:

def check_profanity(text_to_check):
  flag = 0
  super_word = ''
  for i in range(len(text_to_check)):
    if i < 100 and i < len(text_to_check): #100 or max number of words you can check at the same time
      super_word = super_word + " " + word
    else:         
      connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+super_word)
      super_word = ''
      output = connection.read()
      if b"true" in output:   
        flag = flag +1
        break
  if flag > 0:
    print("profanity alert")
  else:
    print("the text has no curse words")

How to Make a Program Run Faster, Updated April 21, 2010 to include new steps for Windows 7. We've all been frustrated when some program on our computer is taking too long  I'm always getting around 12 ms I want to get around 5ms. How can I achieve this? Does using an iterator make a program run faster? class Solution { public: int singleNumber(vector&lt;int&g


First off, as Menno Van Dijk suggests, storing a subset of common known curse words locally would allow rapid checks for profanity up front, with no need to query the website at all; if a known curse word is found, you can alert immediately, without checking anything else.

Secondly, inverting that suggestion, cache at least the first few thousand most common known non-cursewords locally; there is no reason that every text containing the word "is", "the" or "a" should be rechecking those words over and over. Since the vast majority of written English uses mostly the two thousand most common words (and an even larger majority uses almost exclusively the ten thousand most common words), that can save an awful lot of checks.

Third, uniquify your words before checking them; if a word is used repeatedly, it's just as good or bad the second time as it was the first, so checking it twice is wasteful.

Lastly, as MTMD suggests, the site allows you to batch your queries, so do so.

Between all of these suggestions, you'll likely go from a 100,000 word file requiring 100,000 connections to requiring only 1-2. While multithreading might have helped your original code (at the expense of slamming the webservice), these fixes should make multithreading pointless; with only 1-2 requests, you can wait the second or two it would take for them to run sequentially.

As a purely stylistic issue, having read_text call check_profanity is odd; those should really be separate behaviors (read_text returns text which check_profanity can then be called on).

With my suggestions (assumes existence of files with one known word per line, one for bad words, one for good):

import itertools  # For islice, useful for batching
import urllib.request

def load_known_words(filename):
    with open(filename) as f:
        return frozenset(map(str.rstrip, f))

known_bad_words = load_known_words(r"C:\path\to\knownbadwords.txt")
known_good_words = load_known_words(r"C:\path\to\knowngoodwords.txt")

def read_text():
    with open(r"C:\Self\General\Pooja\Edu_Career\Learning\Python\Code\Udacity_prog_foundn_python\movie_quotes.txt") as quotes:
        return quotes.read()

def check_profanity(text_to_check):
    # Uniquify contents so words aren't checked repeatedly
    if not isinstance(text_to_check, (set, frozenset)):
        text_to_check = set(text_to_check)

    # Remove words known to be fine from set to check
    text_to_check -= known_good_words

    # Precheck for any known bad words so loop is skipped completely if found
    has_profanity = not known_bad_words.isdisjoint(text_to_check)
    while not has_profanity and text_to_check:
        block_to_check = frozenset(itertools.islice(text_to_check, 100))
        text_to_check -= block_to_check

        with urllib.request.urlopen("http://www.wdylike.appspot.com/?q="+' '.join(block_to_check)) as connection:
            output = connection.read()
        # print(output)
        has_profanity = b"true" in output

    if has_profanity:
        print("profanity alert")
    else:
        print("the text has no curse words")

text = read_text()
check_profanity(text.split())

How to Get Programs to Open and Run Faster, Do you wish to make your Windows 10 run faster? “End task”, and in the future you could consider an alternative program for the job (Chrome  But it takes half of a day for the program to complete. Are there any basic things I could change about the code which could make the program run faster? Like, currently, each iteration requires about 100 (quite small) function calls. How much faster would the program be if I condensed that number into something more like 10 or 5?


There a few things you can do:

  1. Read batches of text
  2. Give each batch of text to a worker process which then checks for profanity.
  3. Introduce a cache which saves commonly used curse words offline to minimize the amount of required HTTP requests

How to Make Programs Run Faster, Python | Making program run faster. As we know, Python programming language is a bit slow and the target is to speed it up without the assistance of more  19 Tips & Tricks To Speed Up Windows 10 And Make It Faster 1. Prevent programs from loading at startup. 2. Use fewer apps at a time, reduce RAM usage to speed up computer. 3. See if your Windows isn’t corrupt. 4. Scan your PC for malware to speed up Windows 10. 5. Shutting down and restarting


Use multithreading. Read batches of text. Assign each batch to a thread and check all the batches seperately.

How to make programs open and run faster, Want Windows 10 to run faster? (Depending on your make and model, you might see other plans Disable programs that run on startup. 12 Tips to Speed Up Windows 10 Windows 10 is speedier than the previous versions of Microsoft's OS, but you can still optimize its performance. Learn how to make your Windows 10 PC run faster with


Send many words at once. Change number_of_words to the number of words you want to send at once.

import urllib.request

def read_text():
    quotes = open("test.txt")
    contents_of_file = quotes.read().split()
    quotes.close()
    check_profanity(contents_of_file)

def check_profanity(text):
    number_of_words = 200
    word_lists = [text[x:x+number_of_words] for x in range(0, len(text), number_of_words)]
    flag = False
    for word_list in word_lists:
        connection = urllib.request.urlopen("http://www.wdylike.appspot.com/?q=" + "%20".join(word_list))
        output = connection.read()
        if b"true" in output:
            flag = True
            break
        connection.close()
    if flag:
        print("profanity alert")
    else:
        print("the text has no curse words")

read_text()

How to Make Programs Open and Run Faster, To delete temporary files on Windows, you can run a disk cleanup, which you can access by clicking Start > All Programs > Accessories > System  RAM stands for random access memory, and it’s what programs use to run. When there isn’t enough available RAM, your PC slows down. Adding a new stick of RAM or replacing your current sticks with faster and bigger RAM will have a significant effect on the speed of your PC.


7 Ways to Make Windows 10 Run Faster,


Python,


Windows 10 quick tips: 13 ways to speed up your PC,