kozter has asked for the wisdom of the Perl Monks concerning the following question:

I'm VERY new to perl, so you'll have to give me a break here. I just have a question about language of choice. I have assignment to do for school. I haven't used perl before, but I have done scripts. I have an "ok" knowledge of C. I would like to learn some perl, so was thinking of doing this assignment in Perl. But the assignment is also marked on efficiency. So which would be faster in this application? Or what are your suggestions? Thanks Write a simple scheduler called myscheduler that accepts commands along with user ids, and prioritizes their execution based on past history of the users' requests. Specifically,....
your program takes names of two files - infile and outfile - as commmand-line parameters. infile contains a list of commands to execute and details related to each command. This information is provided in the form of (serial number, userid, est-time, command) quadruples, so that each line of infile contains a three digit serial number (beginning with 000), a three digit user id, an estimated amount of cpu time (in milliseconds) required for completing the request, and finally the command to be executed with its options, where the fields are separated by single spaces. Your program reads the entire file, puts the commands (with relevant details) in a queue, ordering the requests according to the shortest job first scheme. The shortest job first scheme carries out commands with lower estimated cpu time requirements before commands with higher estimated cpu time requirements. As myscheduler executes commands, it reorders remaining requests in the queue based on the discrepancy in the estimated amount of cpu time required by the commands which have already been executed. Specifically, after executing a command, if the program sees that the actual amount of cpu time taken is larger than the estimated time, it increases estimated time for each remaining command request by the same user by the same percentage, and then re-orders the queue; however, if the actual time is less than the given estimate, it does not change anything. Your program continues until all commands have been executed, and finally, writes to outfile the order in which the commands were executed (by writing serial numbers of requests in the order in which they were executed - one serial number per line), followed by the average amount of discrepancy in estimates for commands requested by each user (by writing a userid, followed by average amount of discrepancy (in milliseconds) with respect to the estimates provided in infile, followed by discrepancy with respect to the modified estimates at the time of the execution of the commands) - one user's information per line). Fields on the same line in the discrepancy part of outfile should be separated by single spaces.

20041120 Edit by castaway: Changed title from 'Newbie Question'

Replies are listed 'Best First'.
Re: Preferred language for scheduler assignment?
by QM (Parson) on Nov 19, 2004 at 16:55 UTC
    This problem would be straightforward in Perl. Whether it meets your requirements for efficiency depends on the criteria.

    If the code just needs to be efficient (as in not wasteful), then Perl is probably better than C, etc. If it needs to run as fast as possible, then C might be your best bet. Considering that most of the time is spent executing other programs, I think it would be hard to tell the difference between comparable C and Perl implementations.

    I would suggest looking at Time::HiRes (or Time::AutoRes); system or `` (backquotes) or qx//; reading and writing files (the <> operator, and print/printf); split and/or regular expressions (match operator m// or just //).

    But if you're more comfortable in C than in Perl, C will be less hassle for you in the short term. If you have some time to learn, Use Perl!.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      But if you're more comfortable in C than in Perl, C will be less hassle for you in the short term. If you have some time to learn, Use Perl!.
      I have to second that. I like perl, I really do, but when I was in school I tried to use a language I did not know and it cost me. This comment is not about a language war but rather it is about using your time efficiently and meeting as many of your goals as you can.

      It was an intro compiler course. The first assignment was a simple translator and we could use one of five languages (and lex libraries). I was most comfortable with C so I wrote about 1500 lines of code over a two day weekend. gdb was really handy for tracking through pointers in trees. I got a good grade on it.

      Now the recommended language was python. I had never used python but the instructor recommeneded it and so did other students who knew it. I saw some of their programs were shorter than mine and I had spent a lot of time just writing linked list and tree code. So I tried python for the second program in the course.

      The short version is I was busy with a paying job and school and the local undergrad club and now I was trying to write a compiler (which was new to me anyway) in a language I had never even written "hello world" in.

      My assignment was late by a few days and was never completed. Up to that point I had used C, C++, Java, I don't know how many Assembly languages, LISP, Prolog and I'm sure a couple more but for the life of that compiler assignment I couldn't get python figured out in the time I had. (I still got a B-something in the course.)

      My advice is don't take on too much. This course sounds like a Real Time Systems course. It's not on Perl or languages so focus your effort on the course content.

      Afterwards most people are not going to notice that you learned a new language in a course and you can always learn it on your own time. Employers who ask to see your grades will likely notice if you did not pass a course and maybe you would prefer they see a higher grade. That said if you are asking this question you are likely bright and enjoy learning and will probably do just fine but my advice still remains as: don't complicate your assignments (or work projects) any more than you have to.
        Hear! Hear! Well said.

        Just a comment on this:

        Employers who ask to see your grades will likely notice if you did not pass a course and maybe you would prefer they see a higher grade.
        A plum employer would be more interested in why you didn't succeed, what lessons you learned from the experience, and what you would do differently given the chance. You shouldn't be afraid of failing, but afraid of not learning anything in the process, and afraid of not trying again.

        Good judgement comes from experience. Experience comes from bad judgement.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Preferred language for scheduler assignment?
by davido (Cardinal) on Nov 19, 2004 at 17:03 UTC

    Given that Priority Queues (heaps) are implemented by Heap::Simple, and Heap, the queuing portion of the task is mostly implemented for you already. You just have to handle the logic where order is only adjusted when something takes longer, but not when it takes shorter.

    Reading in the flat files and splitting them into individual elements is also one of those things that Perl does easily.

    And the Benchmark module, or Time::HiRes may be what you need for timing the execution of each entity.

    So Perl definately provides the building blocks to you, through CPAN as well as built-in functionality. It starts you off a little further down the development road than C, for example, for a project like this.

    That means that in programming efficiency, Perl is going to beat C on this particular type of project. But that doesn't mean it will be the winner based on execution speed. If raw speed is the goal, dust off your assembler and start bit-fiddling.

    In amount of code, Perl is probably going to come out ahead; you're going to end up typing fewer lines of Perl to accomplish this than lines of C, especially since some of the groundwork is already done and available on CPAN.

    As far as development efficiency, yes, Perl is starting you out further down the road toward "done", but if you don't know Perl, you have to also consider the learning curve.


    Dave

      Given that Priority Queues (heaps) are implemented by Heap::Simple, and Heap, the queuing portion of the task is mostly implemented for you already.

      The way I read the assignment, the instructor is looking for the students to create their own priority queue. The OP should run using Heap::* by the instructor to make sure that it is okay for the class. There is no point in using it if it will hurt the OP's grade.

      Jason L. Froebe

      Team Sybase member

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: Preferred language for scheduler assignment?
by ikegami (Patriarch) on Nov 19, 2004 at 17:06 UTC
    In this case, I think the prof wants you to use an efficient algorithm. I don't think Perl will be a problem. But really, go see your prof! Tell him/her you're interested in practicing your Perl, and ask him/her if he has any problems with you using Perl for this assignment.
      Were it I (and I know it ain't; YMMV, etc), if would second ike's recommendation with these mods:

      "...and ask him/her if (s/)he has any problems with you using Perl (and the CPAN modules mentioned above) for this assignment."

      Yes, first is a nitpicking joke, but, mindful of the "little tin god" warning above, the second may be important.

      good luck!
Re: Preferred language for scheduler assignment?
by trammell (Priest) on Nov 19, 2004 at 16:46 UTC
    I'd argue that the "efficiency" of a program includes the time spent developing and maintaining the code.

    Unfortunately, you're in school, and in my experience most CS professors have some little tin god or other they worship, so be prepared to sacrifice your grades if you think for yourself.