473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sort program runs but is slow

What it does:

runs through a file, sorts, and splits it alphabetically into files with words that are equal or less than 200.

In this example I'm using the dict file, and I use it several times, so excuse the redunancy since I didn't want to include the actual source files.

It works, but it takes a very long time.

Can someone help me get it working faster? Thanks much.

Gzip'd file: http://www.mediafire.com/download.php?zykjlzmktjn


Code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use Text::CSV;
  4. use File::Copy;
  5. use File::stat;
  6.  
  7. use POSIX qw(strftime);
  8.  
  9. my $green_dir  = "tmp/list/green";
  10. my $file_green = "tmp/all_green.txt";
  11.  
  12. my $blue_dir  = "tmp/list/blue";
  13. my $file_blue = "tmp/all_blue.txt";
  14.  
  15. my $green_blue_dir  = "tmp/list/green_blue";
  16. my $file_green_blue = "tmp/all_green_blue.txt";
  17.  
  18. my $allelse_dir  = "tmp/list/allelse";
  19. my $file_allelse = "tmp/allelse.txt";
  20.  
  21. my $all_dir  = "tmp/list/all";
  22. my $file_all = "tmp/all.txt";
  23.  
  24. my $max = 200;
  25.  
  26. $cnt  = 0;
  27. $cnt2 = 2;
  28.  
  29. sub rem_green {
  30.     $buf = "rm -f $green_dir/*";
  31.     system($buf);
  32. }
  33.  
  34. sub rem_blue {
  35.     $buf = "rm -f $blue_dir/*";
  36.     system($buf);
  37. }
  38.  
  39. sub rem_green_blue {
  40.     $buf = "rm -f $green_blue_dir/*";
  41.     system($buf);
  42. }
  43.  
  44. sub rem_allelse {
  45.     $buf = "rm -f $allelse_dir/*";
  46.     system($buf);
  47. }
  48.  
  49. sub rem_all {
  50.     $buf = "rm -f $all_dir/*";
  51.     system($buf);
  52. }
  53.  
  54. @files = (
  55.     'A', 'C', 'B', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  56.     'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  57.     'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  58. );
  59.  
  60. @fl = (
  61.     'A', 'C', 'B', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  62.     'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  63.     'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  64. );
  65.  
  66. if ( $ARGV[0] eq "-o" ) {
  67.     $file_name = $file_blue;
  68.     $file_dir  = $blue_dir;
  69.     rem_blue();
  70. }
  71.  
  72. if ( $ARGV[0] eq "-p" ) {
  73.     $file_name = $file_green;
  74.     $file_dir  = $green_dir;
  75.     rem_green();
  76. }
  77.  
  78. if ( $ARGV[0] eq "-po" ) {
  79.     $file_name = $file_green_blue;
  80.     $file_dir  = $green_blue_dir;
  81.     rem_green_blue();
  82. }
  83.  
  84. if ( $ARGV[0] eq "-a" ) {
  85.     $file_name = $file_all;
  86.     $file_dir  = $all_dir;
  87.     rem_all();
  88. }
  89.  
  90. if ( $ARGV[0] eq "-ae" ) {
  91.     $file_name = $file_allelse;
  92.     $file_dir  = $allelse_dir;
  93.     rem_allelse();
  94. }
  95.  
  96. $files_cnt = 0;
  97. $fl_cnt    = 0;
  98. foreach (@files) {
  99.     $file = $_;
  100.     $file .= "_1.txt";
  101.     chomp($file);
  102.     unlink("$file_dir/$file");
  103.     open( IN, "<", "$file_name" );
  104.     while (<IN>) {
  105.         $word = $_;
  106.         chomp($word);
  107.         foreach (@fl) {
  108.             $fl = $_;
  109.             chomp($fl);
  110.             if ( $word =~ /^[$fl]/ && $file =~ /^[$fl]/ ) {
  111.                 if ( $cnt == $max ) {
  112.                     $file =~ s/_.*//;
  113.                     $file .= "_$cnt2.txt";
  114.                     $cnt2++;
  115.                     $cnt = 0;
  116.                     unlink("$file_dir/$file");
  117.                 }
  118.                                 print "$file_dir $file\n";
  119.                 open( OUT, ">>", "$file_dir/$file" ) or die $!;
  120.                 print OUT "$word\n";
  121.                 close(OUT);
  122.                 $cnt++;
  123.                 $fl_cnt++;
  124.             }
  125.             $fl_cnt = 0;
  126.         }
  127.         $files_cnt++;
  128.     }
  129.     $files_cnt = 0;
  130.     $cnt       = 0;
  131.     $cnt2      = 2;
  132. }
  133. close(IN);
May 9 '10 #1
0 1143

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ollie | last post by:
I am trying to figure out how a complex program written in python works. I am new to python so looking at the source code directly is not helping me much. As such, I am running the program under...
0
by: jwad01 | last post by:
I have a stored procedure that gets executed by a Crystal Report. The report runs in about a minute. Using profiler, I get the SP that was executed by the report and run it in Query Analyzer. In...
11
by: Brett | last post by:
Hi. I wrote a program in C that spends most of its time doing integer arithmetic (on a data set loaded at run time), with a negligible amount of I/O. I compiled it with lcc-win32 as a console...
3
by: happy | last post by:
/* Book name : The prodessional programmers guide to C File name : E:\programs\tc\iti01\ch09\main\01setupm.c Program discription: file setuping -up -Version 01-ver01-W Logic ...
1
by: Steve Bishop | last post by:
I have an application that uses data access that runs slow the first time. My first page reads data from an ODBC source and the second page hits a MSDE database. After each page opens the first...
1
by: Jed | last post by:
I have a class for logging information about user activity on a web site, but it suddenly became really slow. I was wondering what factors may have caused the slow down. Initially, I used the...
6
by: André | last post by:
Hi, I made a webform for a survey, with multiple-choice questions. The results of each question is put into a table e.g.: values frequency 1 6 2 3 3 32 4 ...
2
Atran
by: Atran | last post by:
Hello: I have two programs (Windows Forms). I want to make the second program runs when I press a button in the first program. So when the user go to run the second program, I want the second...
8
by: Jothishankar | last post by:
Hi, I am new to c#. I am trying to build an application that does backup of files to an external hard disk. My application behaves strangely. When i run the application under debug mode (F5),...
5
by: AleksKleyn | last post by:
I recently install web express studio 2008 on win xp, 2 gb memory. I do not have problem when open project with asp files. However when I open project with aspx files web studio runs slow. Each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.