Glynn Clements wrote:
> 
> Markus Neteler wrote:
> > when using g.mlist in mapsets with thousands of maps in
> > it (as happens with MODIS time series), it gets extremely
> > slow.
..
Glynn Clements wrote:
> The main thing which stands out is that it invokes "grep" once for
> every name returned by g.list:


Hi,

see attached patch for consideration. Following Glynn's suggestion it
gets rid of the loop for each map name, making the script ~50% faster
than before, but still about twice as slow as a single call to g.list.

I haven't put it in CVS as I haven't really tested it.


Hamish
Index: g.mlist
===================================================================
RCS file: /home/grass/grassrepository/grass6/scripts/g.mlist/g.mlist,v
retrieving revision 1.16
diff -u -r1.16 g.mlist
--- g.mlist	19 Apr 2007 16:26:12 -0000	1.16
+++ g.mlist	10 Sep 2007 03:47:41 -0000
@@ -56,40 +56,34 @@
 #%end
 
 do_list() {
-    list=""
-    for i in `g.list type=$type mapset=$mapset | grep -v '^-\+$' | grep -v "files available" | grep -vi "mapset"`
-    do
-        if [ ! "$search" ] ; then
-    	list="$list $i"
-        else
-    	list="$list `echo $i | grep \"$search\"`"
-        fi
-    done
+    
+    rawlist=`g.list type="$type" mapset="$mapset" | grep -v '^-\+$\|files available' | sed 's/  */\n/g' | grep -v '^$'`
+    if [ ! "$search" ] ; then
+	list="$rawlist"
+    else
+	list=`echo "$rawlist" | tr ' ' '\n' | grep "$search"`
+    fi
 
     if [ ! "$list" ] ; then
 	return
     fi
 
-    sorted_list=`echo "$list" | tr ' ' '\n' | sort | tr '\n' ' '`
+    sorted_list=`echo "$list" | tr ' ' '\n' | sort`
 
     if [ $GIS_FLAG_M -eq 1 ] ; then
 	MAPSET="@$mapset"
     else
 	MAPSET=""
     fi
-    i=""
-    for i in $sorted_list
-    do
-        if [ "$start" ] ; then
-	    if [ -z "$sep" ] ; then
-		echo
-	    else
-		echo -n "$sep"
-	    fi
-        fi
-        start=1
-        echo -n "$i$MAPSET"
-    done
+
+    if [ "$start" != "TRUE" ] ; then
+	echo -n "$sep"
+    else
+	start="FALSE"
+    fi
+
+    echo -n "$sorted_list" | sed -e "s/$/$MAPSET/" | tr '\n' "$sep"
+
 }
 
 
@@ -109,6 +103,9 @@
 type="$GIS_OPT_TYPE"
 mapset="$GIS_OPT_MAPSET"
 sep="$GIS_OPT_SEP"
+if [ -z "$sep" ] ; then
+   sep='\n'
+fi
 search="$GIS_OPT_PATTERN"
 
 #list all datatypes
@@ -121,11 +118,9 @@
 done 
 
 if [ -z "$sep" ] ; then
-#    echo using "\n" as separator
-    sep=""
+   sep='\n'
 fi
 
-
 if [ $GIS_FLAG_R -ne 1 ] ; then
 #    echo wildcard
     search="^`echo \"$search\" | sed 's/\./\\\\./g; s/?/./g; s/*/.*/g; s/|/$|^/g'`$"
@@ -133,7 +128,7 @@
 #    echo regex, as is
 fi
 
-start=""
+
 if [ -z "$mapset" ] ; then
 #    echo using current mapset
     mapsets=`g.mapsets -p`
@@ -141,8 +136,8 @@
     mapsets=`echo $mapset | sed 's/,/ /g'`
 fi
 
-for mapset in $mapsets
-do
+start=TRUE
+for mapset in $mapsets ; do
     do_list
 done
 
@@ -150,4 +145,3 @@
 echo ""
 
 exit 0
-
_______________________________________________
grass-dev mailing list
[email protected]
http://grass.itc.it/mailman/listinfo/grass-dev

Reply via email to