Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch coding - which command to use?  (Read 11729 times)

0 Members and 1 Guest are viewing this topic.

Hedonist

    Topic Starter


    Intermediate

    Batch coding - which command to use?
    « on: March 17, 2009, 02:34:33 PM »
    Win XP Home.

    When there is more than one command which will produce the desired result how does one select the command to use for best performance.  For example, in the code below, is a For loop preferable to three Set commands when extracting date information?  (date format used is 'day dd-mm-yyyy').

    Code: [Select]
    @echo off
    setlocal
    cls
    :: Demo one....
    echo Results from Demo one..
    for /f "tokens=1-4 delims=- " %%1 in ("%date%") do (
        set day=%%2
        set mth=%%3
        set year=%%4
    )
    echo %day% %mth% %year%

    set day=
    set mth=
    set year=
    set day=%date:~4,2%
    set mth=%date:~7,2%
    set year=%date:~-4%
    echo %day% %mth% %year%

    :: Demo two....
    echo.
    echo Results from Demo two..
    set right=%date:~-10,-8%
    echo %right%

    set right=%date:~4,2%
    echo %right%

    Thanks.

    macdad-



      Expert

      Thanked: 40
      Re: Batch coding - which command to use?
      « Reply #1 on: March 17, 2009, 03:12:35 PM »
      So are you asking that you want a command that you can input what data you want?
      E.g:

      Quote
      Do you want:

      Month:1
      Year:2
      Day:3
      Day of Week:4

      Enter Choice:
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      Hedonist

        Topic Starter


        Intermediate

        Re: Batch coding - which command to use?
        « Reply #2 on: March 17, 2009, 03:41:56 PM »
        No.   If there is more than one command which will produce the desired result how do I select the most efficient one?   In the code I posted there are examples of different commands to give the same result, i.e. the For loop produces the same output as three Set commands but which is the most efficient use of the commands or system features?  In other words is the For loop faster than three Set commands, the same, or slower?

        In Assembler language a Register-to-Register instruction is executed faster than Register-to-Storage, Storage-to-Storage and Storage-to-Register.  Does the same type of circumstance apply to the Command Interpreter and, if so, how does one select the best command to use?

        Thanks for your interest.

        Dias de verano

        • Guest
        Re: Batch coding - which command to use?
        « Reply #3 on: March 17, 2009, 04:44:28 PM »
        If there is more than one command which will produce the desired result how do I select the most efficient one?

        I don't think it really matters. Scripting is by its very nature not used for time-critical tasks, so it is hard to say what "efficient" actually means. Efficient use of the programmer's time comes into the equation.

        Neither of your methods work on my system, so to me their "efficiency" is zero.

        If it really matters to know which method is faster, time how long (say) 250 times each method takes.

        I think the answer is that considerations such as ease of coding, usability and maintainability outweigh speed in those situations where a scripting language is most useful.




        « Last Edit: March 18, 2009, 12:24:38 AM by Dias de verano »

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Batch coding - which command to use?
        « Reply #4 on: March 17, 2009, 05:02:51 PM »
        as an aside even picking a specific language based on speed is a mistake. For example I have seen far to many C DLL's written to interface with VB5 and 6 "For speed"- they have complicated interfaces and require all sorts of helper routines in a VB module to use.

        My point? All the work and you get a free 5 seconds after a million iterations over the same routine coded in VB. I later discovered that they used a simple bubble-sort in the DLL, which meant I was wasting my time, and also explained why I needed to wait well over 2 minutes to sort larger numbers of items.

        The fix? I wrote a quicksort function in VB- time savings? well- instead of O(n^2) I reduced it to a O(Log(n)) algorithm... which is quite a feat. If you don't know about big O notation just read that as much faster.

        the lesson? It isn't the tool or built-in functions that determine your speed- it's how you put them together to implement an algorithm.

        VB has string manipulation functions- two complete sets, one of which has a $ on the end. they run faster then the non $ equivalents. However, using the $ version instead of non-$ version would not guarantee speed- I would still need to choose a fast algorithm, or performance will suffer regardless of which version I use.


        The only time that such "micro optimizations" should be performed is in Tight loops that execute thosands or millions of times, and if your writing batch code that loops millions of times you might want to rethink your approach  :P
        I was trying to dereference Null Pointers before it was cool.

        Hedonist

          Topic Starter


          Intermediate

          Re: Batch coding - which command to use?
          « Reply #5 on: March 17, 2009, 06:12:25 PM »
          Quote from: Dias de verano
          Neither of your methods work on my system, so to me their "efficiency" is zero.

          That's interesting - perhaps because your %date% format is different to mine ???

          Thank you all for your comments.


          ghostdog74



            Specialist

            Thanked: 27
            Re: Batch coding - which command to use?
            « Reply #6 on: March 17, 2009, 06:51:26 PM »
            No.   If there is more than one command which will produce the desired result how do I select the most efficient one? 
            Programming languages, like Python/Perl etc provides profiling features, ie, timing a set of commands/statements in your scripts to see how fast they run internally. If you want to do serious programming work, then switch to a better language.

            Hedonist

              Topic Starter


              Intermediate

              Re: Batch coding - which command to use?
              « Reply #7 on: March 17, 2009, 08:18:24 PM »
              Ghostdog74 - not a very helpful addition to this topic which is about Batch Scripting Commands and their selection not about the pros and cons of different programming languages. 

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: Batch coding - which command to use?
              « Reply #8 on: March 17, 2009, 08:32:52 PM »
              what it boils down to is if there are two sets of commands that perform the same task- unless you are running it thousands of times- it really doesn't matter, especially with your program here, since it only runs once and finishes, and generally finishes instantaneously shaving off a few nanoseconds really isn't important.


              Obviously creating a linked list is a bit difficult- if not impossible in batch, but it's the best example of different implementations of something I could think of...


              And as with optimizing in any language the first thing to look at to speed up code is yourself... the algorithms as well as how you are manipulating data. For example there are many ways of storing a simple list of data- Linked Lists, doubly linked lists, arrays, etc. They all have strong and weak points- a Singly-linked list has a worst case of O(n), since you must go through every item to get to the last one- essentially it's best for lists that will be accessed sequentially. A doubly linked list has a worst case performance of O(n/2), since  the code can start from either the beginning or the end of the list, and the farthest item from the start and end is of course the middle, or halfway. the doubly linked list is great for data that can be accessed forward as well as backward, or when you might need previous items to properly process the current ones.

              Another common method is to simply use an Array. An array-based list offers near instantaneous  addition and access to anywhere in the list- however the big performance loss comes with removing items, since  the entire array needs to be shifted.


              So the linked list is great for data that is accessed sequentially or changes often; chopping off a portion of the list is as easy as setting Head and Tail Pointers to Null and allowing garbage collection or COM reference counting to finish cleaning up the now orphaned remains. An Array-based implementation in the other hand is ideal when you need to access any item in the array NOW and you do fewer removals.


              What am I trying to say? Well, again- it's the method, not the madness, heh.

              Didn't mean to go into another speel...
              I was trying to dereference Null Pointers before it was cool.

              ghostdog74



                Specialist

                Thanked: 27
                Re: Batch coding - which command to use?
                « Reply #9 on: March 17, 2009, 08:56:04 PM »
                Ghostdog74 - not a very helpful addition to this topic which is about Batch Scripting Commands and their selection not about the pros and cons of different programming languages. 
                what makes you think its about pros and cons of different programming languages? you mentioned "how to select the best command to use for best performance" correct?  my answer to you "timing a set of commands/statements in your scripts to see how fast they run internally". If you can find such "timing" facilities in batch(cmd.exe), then go ahead and use it. the most common way to do that is using %time% variable, but to me, that's not even accurate.

                Dias de verano

                • Guest
                Re: Batch coding - which command to use?
                « Reply #10 on: March 18, 2009, 12:23:24 AM »
                That's interesting - perhaps because your %date% format is different to mine ?

                Code: [Select]
                C:\>echo %date%
                18/03/2009

                Hedonist

                  Topic Starter


                  Intermediate

                  Re: Batch coding - which command to use?
                  « Reply #11 on: March 18, 2009, 12:50:20 AM »
                  Code: [Select]
                  C:\>echo %date%
                  18/03/2009

                  Uhuh ..
                  Quote from: Hedonist in Original Post
                  (date format used is 'day dd-mm-yyyy').

                  Code: [Select]
                  c:\echo %date%
                  Wed 18-03-2009

                  So I guess the small scripts I posted would not work for you because the date formats differ.

                  Anyway, this topic has run its course - thanks to all.


                  Dias de verano

                  • Guest
                  Re: Batch coding - which command to use?
                  « Reply #12 on: March 18, 2009, 03:04:58 AM »

                  Anyway, this topic has run its course



                  Not quite...

                  Code: [Select]
                  @echo off
                  setlocal enabledelayedexpansion
                  REM my local date format is dd/mm/yyyy
                  set loops=%1
                  echo Loops: %loops%
                  call :gettick
                  set starttime=%milliseconds%
                  for /L %%N in (1,1,%loops%) do (
                  set day=!date:~0,2!
                  set month=!date:~3,2!
                  set year=!date:~6,4!
                  )
                  call :gettick
                  set finshtime=%milliseconds%
                  set /a elapsed=%finshtime%-%starttime%
                  echo Method (1) slice the string Elapsed: %elapsed% milliseconds
                  call :gettick
                  set starttime=%milliseconds%
                  for /L %%N in (1,1,%loops%) do (
                  for /f "tokens=1-3 delims=/" %%T in ("%date%") do (
                  set day=%%T
                  set month=%%U
                  set year=%%V
                  )
                  )
                  call :gettick
                  set finshtime=%milliseconds%
                  set /a elapsed=%finshtime%-%starttime%
                  echo Method (2) Use tokens       Elapsed: %elapsed% milliseconds
                  goto :end

                  :gettick
                  REM uses Olof Lagerkvist's tickcount.exe
                  REM http://www.ltr-data.se/files/tickcount.zip
                  REM He hosts some handy little utils at
                  REM http://www.ltr-data.se/opencode.html
                  for /f "skip=1 tokens=1-12 delims= " %%A in ('tickcount.exe') do (
                  set weeks=%%A
                  set days=%%C
                  set hours=%%E
                  set minutes=%%G
                  set seconds=%%I
                  set msec=%%L
                  )
                  set /a secs=604800*%weeks%
                  set /a secs=%secs%+(86400*%days%)
                  set /a secs=%secs%+(3600*%hours%)
                  set /a secs=%secs%+(60*%minutes%)
                  set /a secs=%secs%+%seconds%
                  set /a milliseconds=1000*%secs%
                  set /a milliseconds=%milliseconds%+%msec%
                  goto :eof

                  :end

                  Code: [Select]
                  Loops: 100000
                  Method (1) slice the string Elapsed: 14534 milliseconds
                  Method (2) Use tokens       Elapsed: 11062 milliseconds

                  Loops: 200000
                  Method (1) slice the string Elapsed: 29949 milliseconds
                  Method (2) Use tokens       Elapsed: 25382 milliseconds

                  Loops: 500000
                  Method (1) slice the string Elapsed: 68983 milliseconds
                  Method (2) Use tokens       Elapsed: 54623 milliseconds

                  Loops: 1000000
                  Method (1) slice the string Elapsed: 139750 milliseconds
                  Method (2) Use tokens       Elapsed: 113025 milliseconds

                  It has now...

                  ghostdog74



                    Specialist

                    Thanked: 27
                    Re: Batch coding - which command to use?
                    « Reply #13 on: March 18, 2009, 03:31:28 AM »
                     isn't tickcount.exe similar to uptime.exe which measure time since the system is up ?

                    Dias de verano

                    • Guest
                    Re: Batch coding - which command to use?
                    « Reply #14 on: March 18, 2009, 03:45:45 AM »
                    isn't tickcount.exe similar to uptime.exe which measure time since the system is up ?

                    Very similar. Tickcount gives weeks and milliseconds however.

                    Microsoft uptime.exe
                    Code: [Select]
                    \\PUPP-C92F25ED23 has been up for: 0 day(s), 3 hour(s), 34 minute(s), 25 second(s)http://download.microsoft.com/download/winntsrv40/install/uptime_1.01/nt4/en-us/uptime.exe



                    tickcount.exe
                    Code: [Select]
                    System up time:
                    0 weeks 0 days 3 hours 34 minutes 26 seconds and 518 milliseconds.
                    http://www.ltr-data.se/files/tickcount.zip


                    « Last Edit: March 18, 2009, 04:01:28 AM by Dias de verano »

                    BC_Programmer


                      Mastermind
                    • Typing is no substitute for thinking.
                    • Thanked: 1140
                      • Yes
                      • Yes
                      • BC-Programming.com
                    • Certifications: List
                    • Computer: Specs
                    • Experience: Beginner
                    • OS: Windows 11
                    Re: Batch coding - which command to use?
                    « Reply #15 on: March 18, 2009, 04:10:57 AM »
                    hmm.... somebody should write a small program that does the same thing as GetTickCount() and uptime, but instead uses QueryPerformanceFrequency() and QueryperformanceCounter().
                    I was trying to dereference Null Pointers before it was cool.

                    ghostdog74



                      Specialist

                      Thanked: 27
                      Re: Batch coding - which command to use?
                      « Reply #16 on: March 18, 2009, 04:13:21 AM »
                      well, so if it measures time since system is up, how is it possible to measure time elapse between command executions?

                      BC_Programmer


                        Mastermind
                      • Typing is no substitute for thinking.
                      • Thanked: 1140
                        • Yes
                        • Yes
                        • BC-Programming.com
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Beginner
                      • OS: Windows 11
                      Re: Batch coding - which command to use?
                      « Reply #17 on: March 18, 2009, 04:13:50 AM »
                      well, so if it measures time since system is up, how is it possible to measure time elapse between command executions?

                      subtraction.
                      I was trying to dereference Null Pointers before it was cool.

                      Dias de verano

                      • Guest
                      Re: Batch coding - which command to use?
                      « Reply #18 on: March 18, 2009, 04:16:50 AM »
                      well, so if it measures time since system is up, how is it possible to measure time elapse between command executions?

                      You should read the code I posted earlier. I reproduce a relevant portion

                      Code: [Select]
                      set /a elapsed=%finshtime%-%starttime%

                      Dias de verano

                      • Guest
                      Re: Batch coding - which command to use?
                      « Reply #19 on: March 18, 2009, 04:18:25 AM »
                      hmm.... somebody should write a small program that does the same thing as GetTickCount() and uptime, but instead uses QueryPerformanceFrequency() and QueryperformanceCounter().

                      Beware

                      http://www.virtualdub.org/blog/pivot/entry.php?id=106


                      ghostdog74



                        Specialist

                        Thanked: 27
                        Re: Batch coding - which command to use?
                        « Reply #20 on: March 18, 2009, 04:22:20 AM »
                        Ok ... so i see what you are doing. its the same as using %time% except that %time% doesn't give you millseconds.
                        « Last Edit: March 18, 2009, 04:34:52 AM by ghostdog74 »

                        BC_Programmer


                          Mastermind
                        • Typing is no substitute for thinking.
                        • Thanked: 1140
                          • Yes
                          • Yes
                          • BC-Programming.com
                        • Certifications: List
                        • Computer: Specs
                        • Experience: Beginner
                        • OS: Windows 11
                        Re: Batch coding - which command to use?
                        « Reply #21 on: March 18, 2009, 04:33:29 AM »
                        that's not what i meant. What i meant is, you are taking the milliseconds from the tickcount.exe output correct? and what does this milliseconds mean? Is it the time since the system is up and running, or is it the time elapsed between 2 execution of a code. your system may be up since yesterday and assuming you have not shut down the computer, you are  running this code only today. So is tickcount used for the correct purpose?

                        it is the time since the system is running.


                        you run it once. you store that value.

                        you run some code to benchmark.

                        you run it again, storing the new value.

                        you take the difference between the values, thus getting the number of milliseconds that elapsed while the code was running.




                        Anyway, I decided to go ahead and implement a QueryPerformanceCounter program; attached.

                        run it without parameters, and it returns the current value given my QueryPerformanceCounter.

                        run it again, passing in the value returned previously and it spits out the number of seconds that have elapsed... with quite a number of decimals, I might add...






                        [attachment deleted by admin]
                        I was trying to dereference Null Pointers before it was cool.

                        Dias de verano

                        • Guest
                        Re: Batch coding - which command to use?
                        « Reply #22 on: March 18, 2009, 04:34:09 AM »
                        that's not what i meant. What i meant is, you are taking the milliseconds from the tickcount.exe output correct? and what does this milliseconds mean? Is it the time since the system is up and running, or is it the time elapsed between 2 execution of a code. your system may be up since yesterday and assuming you have not shut down the computer, you are  running this code only today. So is tickcount used for the correct purpose?

                        Tickcount measures the time that the system has been up and running. It does not matter if the computer was started yesterday or even last week, since the Int32 value rolls over every 49.8 days.


                        Dias de verano

                        • Guest
                        Re: Batch coding - which command to use?
                        « Reply #23 on: March 18, 2009, 04:38:40 AM »
                        Ok ... so i see what you are doing. its the same as using %time% except that %time% doesn't give you millseconds.

                        Mine does, well tens of milliseconds, which is really what tickcount does. (it only has 10 msec accuracy)

                        Code: [Select]
                        C:\>echo %time%
                        10:37:03.34


                        ghostdog74



                          Specialist

                          Thanked: 27
                          Re: Batch coding - which command to use?
                          « Reply #24 on: March 18, 2009, 04:48:14 AM »
                          Mine does, well tens of milliseconds, which is really what tickcount does. (it only has 10 msec accuracy)

                          Code: [Select]
                          C:\>echo %time%
                          10:37:03.34


                          not that accurate. though i never really got my hands dirty yet, i believe you can get "uptime" through the Win32_OperatingSystem->LastBootUptime. then going through Date formatting to get milliseconds. anyway.....

                          BC_Programmer


                            Mastermind
                          • Typing is no substitute for thinking.
                          • Thanked: 1140
                            • Yes
                            • Yes
                            • BC-Programming.com
                          • Certifications: List
                          • Computer: Specs
                          • Experience: Beginner
                          • OS: Windows 11
                          Re: Batch coding - which command to use?
                          « Reply #25 on: March 18, 2009, 04:51:55 AM »
                          not that accurate. though i never really got my hands dirty yet, i believe you can get "uptime" through the Win32_OperatingSystem->LastBootUptime. then going through Date formatting to get milliseconds. anyway.....

                          you can, but it is simply wrapping around the GetTickCount() API.

                          As shown here:

                          http://msdn.microsoft.com/en-us/library/ms725473(VS.85).aspx

                          chances are any "uptime" properties/routines are merely calling GetTickCount() and processing the result. (actually, chances are they are actually calling GetTickCount64()...
                          I was trying to dereference Null Pointers before it was cool.

                          ghostdog74



                            Specialist

                            Thanked: 27
                            Re: Batch coding - which command to use?
                            « Reply #26 on: March 18, 2009, 04:56:29 AM »
                            so we are suddenly using third-party tools now ? :)

                            BC_Programmer


                              Mastermind
                            • Typing is no substitute for thinking.
                            • Thanked: 1140
                              • Yes
                              • Yes
                              • BC-Programming.com
                            • Certifications: List
                            • Computer: Specs
                            • Experience: Beginner
                            • OS: Windows 11
                            Re: Batch coding - which command to use?
                            « Reply #27 on: March 18, 2009, 05:29:25 AM »
                            whaddya mean?
                            I was trying to dereference Null Pointers before it was cool.